Reputation: 33
I need to call the show button via code but in page we have multiple show buttons without id. How to hit a particular "show" button suppose 3rd show?
Upvotes: 1
Views: 50
Reputation: 12629
You can use jquery selector
$('input[type=submit]')
to get all input
which has type=submit
. More over you need to get 3rd submit button then use :nth(2)
like $('input[type=submit]:nth(2)')
. :nth
will return element based on starting index with 0.
Learn more about jquery selector here
You can check it below. You should be using submitButton.click()
instead of submitButton.val()
. I have used submitButton.val()
for just display purpose.
function getThirdSubmitButton() {
var submitButton = $('input[type=submit]:nth(2)');
alert(submitButton.val())
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<table>
<tr>
<td>
<input type="submit" value="Show 1" />
</td>
</tr>
<tr>
<td>
<input type="submit" value="Show 2" />
</td>
</tr>
<tr>
<td>
<input type="submit" value="Show 3" />
</td>
</tr>
<tr>
<td>
<input type="submit" value="Show 4" />
</td>
</tr>
</table>
<input type="button" value="Get 3rd submit button" onclick="getThirdSubmitButton()" />
Upvotes: 0
Reputation:
Assuming there is no way of uniquely identifying each submit (far, far from ideal) - you can simply reference the index:
document.querySelectorAll('[type="submit"]')[2]
If you have jQuery on the page you may also write:
$('[type="submit"]')[2]
Again, to reiterate, this is not a great way of achieving this at all - but given restrictions you have over the mark-up, it answers the question you're asking.
As an aside, the documents themselves may not have id
attributes, but try and see if there is something uniquely identifiable about one of the parent elements perhaps, and traverse from there.
Upvotes: 1