tags:

views:

205

answers:

2

Currently, I am using an with ajax to update my mysql. Now, I have to click on the button with the mouse for it to work (I am using onclick), but how can I make it accept the "enter" button? My guess is... Enter isn't working because isn't there. If I leave it there, my ajax just doesn't move.

A: 

If you're in an HTML form, then enter will submit the data. You only have to write in the onsubmit event handler to do your ajax calls.

If you're not in an HTML form, check for the key press event handler. Maybe it's what you're looking for.

vIceBerg
Which way is better? I wouldn't mind using form.
Doug
Use the form then
vIceBerg
A: 

Use a <input type="submit> instead of a <button> or type="button" and then hook into the forms' onsubmit event.

For example:

<form action="#" onsubmit="alert('Hello world!'); return false">
<input type="text" name="myText" id="myText" />
<input type="submit" value="Submit" />
</form>

The return false ensures that the browser doesn't actually submit a form.

Matthew Scharley
Perfect! Thanks!
Doug
As a sidenote, in this example, you don't *need* the button. Pressing enter will always work. That said, from a usability point of view, not providing a button is a big problem.
Matthew Scharley