Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click a specific submit button with JQuery

I am clicking a submit button using this:

$('input[type=submit]').click();  

The problem is that I have more that 1 submit button on my page so I need to target a specific submit button.

How could I do that?

like image 957
Satch3000 Avatar asked Nov 30 '11 00:11

Satch3000


People also ask

Which submit button was clicked jQuery?

activeElement will give you the submit button that was clicked. document. activeElement. getAttribute('value') will give you that button's value.

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 can create Submit button in jQuery?

Answer: Use the jQuery submit() Method You can use the submit() method to submit an HTML form (i.e. <form> ) using jQuery. The jQuery code in the following example will submit the form on click of the button (i.e. the <button> element) which has the type attribute set to button (i.e. type="button" ).


1 Answers

If you know the number of submit inputs and which one (in order) you want to trigger a click on then you can use nth-child() syntax to target it. Or add an ID or a class to each one that separates them from the other.

Selecting the elements by their index:

$('input[type="submit"]:nth-child(1)').trigger('click');//selects the first one $('input[type="submit"]:nth-child(2)').trigger('click');//selects the second one $('input[type="submit"]:nth-child(100)').trigger('click');//selects the 100th one 

There are actually several ways to do this including using .eq(): http://api.jquery.com/eq

Selecting the elements by their id:

<input type="submit" id="submit_1" /> <input type="submit" id="submit_2" /> <input type="submit" id="submit_100" />  <script> $('#submit_100').trigger('click'); </script> 

Note that .click() is short for .trigger('click').

like image 168
Jasper Avatar answered Nov 10 '22 02:11

Jasper