Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery for getting Click event on a Table row

I have the following table

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

How can i set an alert message if i clicked on any of the column of <tr class="rows"> using jquery?

like image 236
Nithesh Narayanan Avatar asked Jul 25 '11 13:07

Nithesh Narayanan


2 Answers

You can use delegate for better performance which will attach click event to root container of rows i.e table

$(document).ready(function(){
    $("tableSelector").delegate("tr.rows", "click", function(){
        alert("Click!");
    });
});
like image 114
ShankarSangoli Avatar answered Sep 27 '22 00:09

ShankarSangoli


$(
  function(){
      $(".rows").click(
        function(e){
            alert("Clicked on row");
            alert(e.target.innerHTML);
        }
      )
  }
)

Example

Better solution

$(document).on("click","tr.rows td", function(e){
    alert(e.target.innerHTML);
});
like image 31
epascarello Avatar answered Sep 26 '22 00:09

epascarello