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');
});
The :submit selector selects button and input elements with type=submit. If a button element has no defined type, most browsers will use it as a button with type=submit. Tip: Using input:submit as a selector will not select the button element.
In javascript onclick event , you can use form. submit() method to submit form. You can perform submit action by, submit button, by clicking on hyperlink, button and image tag etc. You can also perform javascript form submission by form attributes like id, name, class, tag name as well.
To submit the form using 'Enter' button, we will use jQuery keypress() method and to check the 'Enter' button is pressed or not, we will use 'Enter' button key code value. Explanation: We use the jQuery event. which to check the keycode on the keypress.
To submit a form using jQuery click event, you need to detect the submit event. Let’s submit a form using click event and without using click event. You can try to run the following code to learn how to submit a form using jQuery click event −
You use this type to add a submit button to forms. When a user clicks it, it automatically submits the form. It takes a value attribute, which defines the text that appears inside the button. An input with a type set to button creates a button, which can be manipulated by JavaScript's onClick event listener type.
For example, if a command button's Caption property is set to &Go, pressing Alt+G triggers the event. On a form, this event occurs when the user clicks a blank area or record selector on the form. Typically, you attach a Click event procedure or macro to a command button to carry out commands and command-like actions.
An input with a type set to button creates a button, which can be manipulated by JavaScript's onClick event listener type. It creates a button just like an input type of submit, but with the exception that the value is empty by default, so it has to be specified. <input type="button" value="Submit" />
This 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/)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With