Reputation: 43
I have made a table with one button in each td and 4 td in each tr, I want to get the sequence of the td when the corresponding button is pressed, but I am getting sequence 0-4 because parent changes after next row. Is there a way to get the sequence of buttons from 0-11. here is my jQuery code.
$('.act-butn').on('click', function() {
var btnClicked = $(this).parent().index();
$('.activity1').text(btnClicked);
});
and here is my Table
<table class="beta" width=443 id="logButtons">
<center>
<tr>
<td><input type='button' name='Production' value='Production' id="actB1" class="act-butn" /></td>
<td> <input type='button' name='Break' value='Break' id="actB2" class="act-butn" /></td>
<td> <input type='button' name='Medical' value='Medical' class="act-butn" /></td>
<td> <input type='button' name='Comfort Break' value='Comfort Break' class="act-butn" /></td>
</tr>
<tr>
<td> <input type='button' name='Meeting' value='Meeting' class="act-butn" /></td>
<td> <input type='button' name='Training' value='Training' class="act-butn" /></td>
<td> <input type='button' name='Business Improvement' value='Bus. Improvement' class="act-butn" /></td>
<td> <input type='button' name='Idle' value='Idle' class="act-butn" /></td>
</tr>
<tr>
<td> <input type='button' name='Downtime' value='Downtime' class="act-butn" /></td>
<td> <input type='button' name='Follow up' value='Follow up' class="act-butn" /></td>
<td> <input type='button' name='one-to-one' value='one to one' class="act-butn" /></td>
<td> <input type='button' name='Coaching' value='Coaching' class="act-butn" /></td>
</tr>
</center>
</table>
Please suggest changes.
Upvotes: 0
Views: 102
Reputation: 171669
If you want to get the <input>
index you need a collection of all those inputs to index against
var $inputs = $('.act-butn').on('click', function() {
// index of `this` in `$inputs` collection
var btnClicked = $inputs.index(this);
$('.activity1').text(btnClicked);
});
Or if you want <td>
index within all <td>
based on input clicked:
var $td = $('.act-butn').on('click', function() {
var btnClicked = $td.index( $(this).parent() );
$('.activity1').text(btnClicked);
}).parent();
Upvotes: 1