Reputation: 11755
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
Reputation: 207501
$(
function(){
$(".rows").click(
function(e){
alert("Clicked on row");
alert(e.target.innerHTML);
}
)
}
)
Better solution
$(document).on("click","tr.rows td", function(e){
alert(e.target.innerHTML);
});
Upvotes: 13
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
Reputation: 3055
$(document).ready(function(){
$("tr.rows").click(function(){
alert("Click!");
});
});
Upvotes: 5