How to set the default button of a form using javascript or jQuery??
-
6What do you mean by *"default button"*? Please add more information. – Felix Kling Oct 29 '11 at 10:55
-
1Already answered here: http://stackoverflow.com/questions/2522125/jquery-ui-dialog-make-a-button-in-the-dialog-the-default-action-enter-key – bilash.saha Oct 29 '11 at 10:59
5 Answers
$(document).ready(function ()
{
$(".tbPassword").keydown(function (e) /* or keyup */
{
if (e.keyCode == 13) // 27=esc
{
$("form").submit();
}
});
});

- 144,742
- 138
- 468
- 792
I know this is an old one, but having spent a good amount of time trying to figure it out (we have a number of different textboxes and buttons) this seemed to be the most straightforward method based on them hitting 'Enter' whilst still in the last input
$(document).keypress(function (e) {
if (e.keyCode === 13) {
if ($("#inputLoginPassword").is(":focus")) {
$("#LoginButton").click();
}
if ($("#inputIntegratedPassword").is(":focus")) {
$("#integratedLoginButton").click();
}
if ($("#inputPinNumber").is(":focus")) {
$("#PinLoginButton").click();
}
return false;
}
});

- 1,603
- 2
- 27
- 52
I'v got the answer from the web URL JQuery: Setting Default Submit Button For ‘Enter Key’ Using JQuery
which he writes the following script:
<div>
<input type="submit" name="CancelButton" value="Cancel" />
<input type="submit" name="DeleteButton" value="Delete" />
<input type="submit" name="UpdateButton" value="Update" />
</div>
Now if you have your focus on one of the HTML form fields – First Name or Last Name and hit ‘Enter Key’, form will be submitted to one of the button actions but you are not sure which ones!
Now let us say you wanted to default the form submission always to “Update” button, you need to do the following –
1- Assign an ID to Update button
<input type=”submit” name=”UpdateButton” value=”Update” id=”defaultActionButton”>
2 -Capture onKeyDown event for textboxes and submit the form to “Update” button / action. [this is where I use jQuery]
// all jQuery events are executed within the document ready function
$(document).ready(function() { $("input").bind("keydown", function(event) {
// track enter key
var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
if (keycode == 13) { // keycode for enter key
// force the 'Enter Key' to implicitly click the Update button
document.getElementById('defaultActionButton').click();
return false;
} else {
return true;
}}); // end of function }); // end of document ready

- 1,519
- 15
- 16
-
This didn't work: document.getElementById('defaultActionButton').click(); I also tried: document.getElementById('defaultButton').click(); -- didn't work either. – John Foll Jun 23 '23 at 18:49
$("form").append('<input type="submit" value="OK" />');
allows you to add a default submit button to all your forms, using jQuery. You can tweak this button a lot before appending it

- 3,301
- 5
- 35
- 33
make a div (with id) in the place where do you want to place the button.
<div id="placeHere"></div>
and from jQuery do this.
$("#placeHere").html('<input type="submit" value="Submit" />')
Hope his helps

- 3,995
- 3
- 25
- 36