0

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");
});
kannan D.S
  • 1,107
  • 3
  • 17
  • 44

3 Answers3

1
$("#0~td").on('click',function(){
alert("clicked");

});

0

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");
})

Demo

T J
  • 42,762
  • 13
  • 83
  • 138
0

Use the following query I hope it solves your query,

var dataa=$("th#0").siblings('td');
dataa.on('click', function(){
alert("clicked");
});

Here is a DEMO

Sam1604
  • 1,459
  • 2
  • 16
  • 26