Say that I have three “button” elements in one parent “div”. Assuming that these buttons could be labeled 1, 2, 3 or 0, 1, 2, using jQuery, is there an easy way to determine the number of which button is clicked?
Here’s the HTML:
<div class="table-tabs">
<button class="tab1 selected">Description</button>
<button class="tab2">Monitoring</button>
<button class="tab3">Change Logs</button>
</div>
I would like to know the number so that I can activate the appropriate table when a button is pressed.
You can use
.index()to get the number of the button (staring at 0).DEMO: http://jsfiddle.net/UPKgm/
Or, you can parse the
classand get the tab number, but that takes more work.DEMO: http://jsfiddle.net/UPKgm/1/