Reputation: 1101
How to alert table data where corresponding table head id="0". I have a dynamic table here with many table heads. one table head id is 0. how can i make an alert when clicked on table data corresponding table head.
<table>
<tr>
<th>heading1</th>
<th id="0">heading2</th>
<td>data1</td>
<td>data2</td>
<td>data3</td>
<td>data4</td>
<td>data5</td>
<td>data6</td>
</tr>
<tr>
<th>heading11</th>
<th id="1">heading12</th>
<td>data1</td>
<td>data2</td>
<td>data3</td>
<td>data4</td>
<td>data5</td>
<td>data6</td>
</tr>
</table>
$("th#0 td").live('click',function(){
alert("clicked");
});
Upvotes: 0
Views: 66
Reputation: 43156
Looks like most of the other answers assumes that <th>
and <td>
containing data are siblings (Well it is, in the given HTML)
The proper table structure should be like
<table>
<thead>
<tr>
<th>heading1</th>
<th id="0">heading2</th>
<th>heading11</th>
<th id="1">heading12</th>
</tr>
</thead>
<tbody>
<tr>
<td>data1</td>
<td>data2</td>
<td>data3</td>
<td>data4</td>
<td>data5</td>
<td>data6</td>
</tr>
<tr>
<td>data1</td>
<td>data2</td>
<td>data3</td>
<td>data4</td>
<td>data5</td>
<td>data6</td>
</tr>
</tbody>
</table>
You can do this:
var index= $("#0").index();
$("table").on("click", "tr td:nth-child("+(++index)+")", function () {
alert("click");
})
Upvotes: 0