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?
06 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 oneThere are actually several ways to do this including using .eq():
Selecting the elements by their id:
<input type="submit" />
<input type="submit" />
<input type="submit" />
<script>
$('#submit_100').trigger('click');
</script>Note that .click() is short for .trigger('click').
If you add a marker, like a specific id or class to your input, you can make your selector more specific. For example, if you give the button you want the ID of form-btn like this:
<input type="submit" />You can select it like this:
$('input[type=submit]#form-btn').click();Or just:
$('#form-btn').click(); Add ids to each button and select the id with jQuery.
Or, if the forms have ids, then just target the form and submit it like so:
$("#form-id").submit(); Way late to the party, but if your submit buttons have names:
$("input[name='submitbuttonname']").click(); One other suggestion!
I had a form with multiple submits, that also had varying value attributes, so I couldn't simply submit the form after I performed my built in confirmation.
Instead, I added a few hidden buttons that I forced a click on after I got a Yes back from my confirmation.
Here are the buttons:
<button name="email" type="submit" value="all"><?php echo $this->__('Email Receipts to Selected'); ?></button>
<button name="email-french" type="submit" value="all"><?php echo $this->__('Email French Receipts to Selected'); ?></button>
<button name="email" type="submit" value="all"></button>
<button name="email-french" type="submit" value="all"></button>And here's the jQuery:
<script type="text/javascript">
jQuery(function ($) { $('[id^=email]').on('click', function (e) { e.preventDefault(); var button = $(this); notify.confirm(<?php echo json_encode($this->__('Are you sure?')); ?>, function (ans) { if (ans) { $('#helper-' + button.attr('id')).click(); } }); });
});
</script> hmm it cant work with a wizard form with this intacted
$("#renqform").validate().settings.ignore = ":disabled";
return $("#renqform").valid();