Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery click on table cell event

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?

like image 629
namco Avatar asked Jun 09 '11 09:06

namco


2 Answers

You need to use event.stopPropagation()

$("tr#info td").click(function(e){     //function_td
  $(this).css("font-weight","bold");
  e.stopPropagation();
});
like image 96
BenMorel Avatar answered Sep 20 '22 21:09

BenMorel


$("tr#info td").click(function(e){     //function_td
  $(this).css("font-weight","bold");
  e.stopPropagation();
});

http://api.jquery.com/event.stopPropagation/

like image 23
Amjad Masad Avatar answered Sep 20 '22 21:09

Amjad Masad