If I have the following form, how can I select the submit button based on the form ID to be used for a click event?
<form id="login">
<input type="text" name="email">
<input type="text" name="password">
<input type="submit" name="submit">
</form>
Something like the following works, but it can't just be input[name=submit]
because there may be more than one on the page.
$('input[name=submit]').click(function (e) {
e.preventDefault();
console.log('clicked');
});
If I have the following form, how can I select the submit button based on the form ID to be used for a click event?
<form id="login">
<input type="text" name="email">
<input type="text" name="password">
<input type="submit" name="submit">
</form>
Something like the following works, but it can't just be input[name=submit]
because there may be more than one on the page.
$('input[name=submit]').click(function (e) {
e.preventDefault();
console.log('clicked');
});
Share
Improve this question
asked Nov 16, 2012 at 0:19
MotiveMotive
3,1119 gold badges43 silver badges63 bronze badges
3 Answers
Reset to default 8This will automatically select the form's submit control:
$('#login :submit').click(...);
http://api.jquery.com/submit-selector/
Cheers
Use :
document.querySelectorAll("input[type=submit]")[0].click();
Use
$('#login input[type="submit"]')
You can read http://www.w3.org/TR/selectors/ for all CSS selectors.. (jQuery supports all default selector + more http://api.jquery.com/category/selectors/)