Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding Parent's Element Click Event from Child Element

Tags:

jquery

I have following HTML:

<table>
    <tr>
        <td><div>test</div></td>
        <td>Some Text</td>
    </tr>
</table>

Using JQuery, I have some code that runs when user click on TR element. It works well.

However, when someone click the DIV inside the TD element, I want only the "click" event of the DIV and not the parent's TR "click" event.

How can I do that?

Thanks all in advance!

like image 594
Henry Aung Avatar asked Jun 24 '11 23:06

Henry Aung


2 Answers

use stopPropagation:

$(function() {
    $('tr').bind('click', function (e) {
        console.log('tr');
    });
    $('div').bind('click', function(e) {
        e.stopPropagation();
       console.log('div');
   });

});

jsfiddle

like image 96
jzilla Avatar answered Sep 20 '22 18:09

jzilla


event.stopImmediatePropagation() worked for me :)

like image 38
SM Adnan Avatar answered Sep 21 '22 18:09

SM Adnan