Get id of selected row in table -HTML

I have a table in my MVC application. I want to get the id of the selected row. I captured the tr click event using jQuery. An example table is shown below.

<table id="resultTable"> <tr id="first"> <td>c1</td> <td>c2</td> </tr> <tr id="second"> <td>c3</td> <td>c4</td> </tr> </table> 

I use the following script to access the event with a mouse click

  $(document).ready(function () { $('#resultTable tr').click(function (event) { alert(this.); //trying to alert id of the clicked row }); }); 

But its not working. How to get the selected row id. Any ideas?

+6
jquery html
source share
2 answers
 $(document).ready(function () { $('#resultTable tr').click(function (event) { alert($(this).attr('id')); //trying to alert id of the clicked row }); }); 
+18
source share
You were almost there. Just contact him directly:
 alert(this.id); 
+6
source share

All Articles