Nithesh Narayanan
Nithesh Narayanan

Reputation: 11755

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?

Upvotes: 11

Views: 77225

Answers (4)

epascarello
epascarello

Reputation: 207501

$(
  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);
});

Upvotes: 13

ShankarSangoli
ShankarSangoli

Reputation: 69905

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

Upvotes: 19

Svetlin Panayotov
Svetlin Panayotov

Reputation: 620

$(".rows").click(function (){ 
   alert('click');
});

Upvotes: 2

Fender
Fender

Reputation: 3055

$(document).ready(function(){
    $("tr.rows").click(function(){
        alert("Click!");
    });
});

Upvotes: 5

Related Questions