I have a html code like below
<tbody>
<tr id="info">
<td class="name">Joe</td>
<td class="surname">White</td>
<td class="age">25</td>
</tr>
</tbody>
and there is a jQuery code like this:
$("tr#info").click(function() { // function_tr
$(this).css("background-color","yellow");
});
$("tr#info td").click(function() { // function_td
$(this).css("font-weight","bold");
});
When I click on the td
, function_td
works fine but function_tr
also works.
How to do prevent function_tr
?
You need to use event.stopPropagation()
$("tr#info td").click(function(e){ //function_td
$(this).css("font-weight","bold");
e.stopPropagation();
});
$("tr#info td").click(function(e){ //function_td
$(this).css("font-weight","bold");
e.stopPropagation();
});
http://api.jquery.com/event.stopPropagation/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With