Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Absolute button control by javascript

Status
Not open for further replies.

ruffy

Programmer
Oct 1, 2003
72
US
My PHP code goes awry when the user hits ENTER
after putting the cursor in a form's input field.
I'd disable the fields, but I want to enable the user
to update the data.

But I only want him to update the stuff after he hits the "Update" button.

How do I ensure via javascript that nothing happens if he
hits the ENTER key inside one of the form fields,
and only the UPDATE button can yield the update?
 
In the form tag add onsubmit="return false" and in the Update button add onclick="this.form.submit()". This should work because calling the submit() method doesn't fire the onsubmit event.

Adam
 
---------------------------------------------------------------------
That didn't work because all my buttons are input type = "button".
I think the reason I did that was to give all my buttons different functions
when clicked. And I do not have a formal "Submit" button.

I tried your suggestion of adding a "return false" to an "onsubmit" event
that I attached to each button, but it was ignored, probably
because no formal input type = "submit" was submitted.

Is there a way I can control the ENTER event from acccessing keyboard input?
---------------------------------------------------------------------
 
Add onsubmit="return false;" to the form, not the buttons. The buttons aren't submitted, the form is.

Lee
 
Do you only have one input of type="text" in your form? If you only have one text field in a form then the form will submit when you hit enter while that field has focus - this is default browser behavior.

If this is the root of your problem then you can add a second text field to the form and use css to hide it. That will disable the auto-submit "feature" you're witnessing.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Trolacious - I like to keep my styling and javascript external to my php & html code.
So although the form is submitted, I capture the event by a button's event handler.

Kaht - I've got many textfields in the form.
On any of these, were I to hit ENTER, the browser submits the form, leading to errors.

Is there a way to capture the ENTER event with javascript?
 
Is there a way to capture the ENTER event with javascript?

Yes, it involves using the onkeydown event defined for the body of the website. There are 1000s of examples out there already. A simple google search should get you what you're looking for.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top