'Use Javascript to change which submit is activated on enter key press
I have a form on an HTML page with multiple submit buttons that perform different actions. However, when the user is typing a value into a text input and hit enters, the browsers generally act as though the next submit button sequentially was activated. I want a particular action to occur, so one solution I found was to put in invisible submit buttons into the HTML directly after the text inputs in question, like this:
<input type="text" name="something" value="blah"/>
<input type=submit name="desired" value="Save Earth" style="display: none"/>
...
<input type=submit name="something_else" value="Destroy Earth" />
...
<input id="foobar" type=submit name="desired" value="Save Earth" />
This works like a charm in most browsers, except that it doesn't in webkit browsers like Safari and Chrome. For some reason they skip over the invisible submit button. I've been trying to figure out how to intercept the enter key press and activate the proper submission using Javascript, but I haven't been able to get it to work. Intercepting the keydown and setting focus on the proper submit does not work.
Is there any way using Javascript or otherwise to select which submit button will be used when the user hits the enter key in a text input on an HTML form?
Edit: To clarify, the form can't require Javascript to "work" fundamentally. I don't care if the enter key submission is undesireable without Javascript on webkit browsers, but I can't remove or change the order of the submit buttons.
This is what I tried, it doesn't change the submission behavior in webkit browsers.
What worked is to change the focus() in the following code to click().
document.onkeypress = processKey;
function processKey(e)
{
if (null == e)
e = window.event ;
if (e.keyCode == 13) {
document.getElementById("foobar").click(); // previously: focus()
}
}
EDIT: FINAL SOLUTION:
Works with every browser and only intercepts the enter key when needed:
HTML:
<input type="text" name="something" value="blah"
onkeydown="return processKey(event)" />
<input type=submit name="desired" value="Save Earth" style="display: none"/>
...
<input type=submit name="something_else" value="Destroy Earth" />
...
<input id="foobar" type=submit name="desired" value="Save Earth" />
Javascript:
function processKey(e)
{
if (null == e)
e = window.event ;
if (e.keyCode == 13) {
document.getElementById("foobar").click();
return false;
}
}
Solution 1:[1]
The invisible default submit action is a reasonable approach and doesn't drag JavaScript into the equation. However form elements with 'display: none' aren't generally reliable. I tend to use an absolutely-positioned submit button before the others on the page, positioned off the left-hand-side of the page. It's still pretty ugly, but gets the job done.
Solution 2:[2]
How about using CSS? You can just set the first button, your default button outside of the visible area of the screen with a "position: absolute; left: -200px; top: -200px;". As far as I remeber, it will not be ignored by any browser, because it is not invisible. This works just fine:
<form method="get">
<input type="text" name="something" value="blah"/>
<input type=submit name="desired" value="Save Earth"
style="position: absolute; left: -200px; top: -200px;"/>
...
<input type=submit name="something_else" value="Destroy Earth" />
...
<input id="foobar" type=submit name="desired" value="Save Earth" />
</form>
And the earth gets saved ...
Solution 3:[3]
Just control the key press event. put onKeyPress in the body tag
onkeypress=CheckKeyPress()
Then handle the enter key
function CheckKeyPress(){
var code;
if(!e) var e = window.event;
if(e.keyCode){
code = e.keyCode;
}else if(e.which){
code = e.which;
}
if(code == 13){
//Do Something
}
}
Solution 4:[4]
Don't intercept the keydown event, intercept the submit event.
so:
form.onsubmit = function(e){
// Do stuff. Change the form's action or method maybe.
return true;
}
Solution 5:[5]
What about using separate forms? Maybe I'm missing something which makes this impossible :P
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | bobince |
| Solution 2 | sth |
| Solution 3 | Tester101 |
| Solution 4 | Kenan Banks |
| Solution 5 | finpingvin |
