Say I have it
<table border="1" id="tbl">
<tr>
<td><input type="checkbox" name="vehicle" value="Bike" /></td>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td><input type="checkbox" name="vehicle" value="Bike" /></td>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>
Now I want to get the checked string, and then the cell values ββof this checked string. Therefore i would do it
var cells = $('#tbl :checked').parents('tr').children('td');
So, let's assume that only one checkbox can be checked (therefore there is no jQuery foreach loop).
So now Iβll say that I want to get the cell value of the 2nd table, I would just go
var secondCell = $(cells[1]).html();
The thing with this, though, is making the code so fragile. For example, if I placed another cell in the table after the checkbox is checked?
<table border="1" id="tbl">
<tr>
<td><input type="checkbox" name="vehicle" value="Bike" /></td>
<td> I am new </td>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td><input type="checkbox" name="vehicle" value="Bike" /></td>
<td> I am new </td>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>
So now I need to go through my code and change this
var secondCell = $(cells[1]).html();
to that
var thirdCell = $(cells[2]).html();
Since then, I am already after the third cell, and not the second cell anymore.
So is there a better way?
thank