cell1cell2How can...">

JQuery to get Click event in table row

I have the following table

<table> <tr class="rows"><td>cell1</td><td>cell2</td></tr> </table> 

How can I set a warning message if I clicked on any of the column of <tr class="rows"> using jquery?

+8
javascript jquery
source share
4 answers

You can use a delegate to improve performance, which will attach the click event to the root container of the rows ie table

 $(document).ready(function(){ $("tableSelector").delegate("tr.rows", "click", function(){ alert("Click!"); }); }); 
+14
source share
 $( function(){ $(".rows").click( function(e){ alert("Clicked on row"); alert(e.target.innerHTML); } ) } ) 

Example

The best solution

 $(document).on("click","tr.rows td", function(e){ alert(e.target.innerHTML); }); 
+10
source share
 $(document).ready(function(){ $("tr.rows").click(function(){ alert("Click!"); }); }); 
+5
source share
 $(".rows").click(function (){ alert('click'); }); 
+1
source share

All Articles