29 Dec

jQuery – Add a default button to a page

Normally in HTML you can add a ‘input type=”submit”‘ to a page and it acts as a default button. However this only works if the input tag is within a ‘form’. I was using AJAX and found that adding a form caused the AJAX to fail. Instead I ended up with the following code:

$(‘input’).keyup(function(e) {
//alert(e.keyCode);
if(e.keyCode == 13) {
$(‘#btnSubmit’).click() ;
}
});

This adds to all input fields a keyup function that checks for the enter key and if found calls the click function on the button I want to use as the default button – in this case it’s an input button called ‘btnSubmit’.

One thought on “jQuery – Add a default button to a page

Leave a Reply

Your email address will not be published.