Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select submit button of specific form for click event

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');

    });
like image 640
Motive Avatar asked Nov 16 '12 00:11

Motive


People also ask

How do I select a submit button?

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.

Can a submit button have onclick?

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.

How do you submit a form on click?

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.

How to submit a form using jQuery click event?

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 −

How to add a submit button to a form using JavaScript?

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.

How do I trigger a click event on a form?

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.

How do I create a button using onClick event in JavaScript?

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" />


3 Answers

This will automatically select the form's submit control:

$('#login :submit').click(...);

http://api.jquery.com/submit-selector/

Cheers

like image 59
Madbreaks Avatar answered Oct 09 '22 06:10

Madbreaks


Use :

 document.querySelectorAll("input[type=submit]")[0].click();
like image 39
Wasif Shahjahan Avatar answered Oct 09 '22 06:10

Wasif Shahjahan


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/)

like image 42
Gabriele Petrioli Avatar answered Oct 09 '22 07:10

Gabriele Petrioli