0

I have a html form with input type=text which launches a custom js function on submit. When the user hits ENTER, a jQuery listener calls the same function.

This means: The user can choose between pressing the submit button and hitting ENTER.

As my js function lets the user stay on the current page, the cursor stays active in the text field which was last focused. So a user could keep hitting ENTER and fire up the js function all the time. Of course I could try to prevent that with a boolean like "didEnterFunction". But can I force the cursor to leave the text field? That would be more elegant.

I imagine something like this:

<form method="post" onsubmit="return myJS();">
    <input type="text" name="request" />
    <input type="submit" value="OK" />
</form>

and

function myJS() {
    // Make the cursor leave the text field "request"
    ?????

    return no; // prevent submission of the form (reload)
}
andreas
  • 7,844
  • 9
  • 51
  • 72

1 Answers1

0

Try this:

function myJS() {
    // Make the cursor leave the text field "request"
    $('input[type="submit"]').focus();

    return false; // prevent submission of the form (reload)
}
palaѕн
  • 72,112
  • 17
  • 116
  • 136
  • I think the idea is the right one. However moving the focus to the submit button will submit the form again when I hit enter! – andreas Mar 31 '13 at 20:01