8

I have a structure like this:

<tr>
 <td onClick="doSomeStuff();">
  <a href="#" onClick="doOtherStuff(1);">1</a>
  <a href="#" onClick="doOtherStuff(2);">1</a>
 </td>
</tr>

My problem is that doSomeStuff() always executes, no matter that I am clicking on a <a href> element.

How I can fix this?

GSerg
  • 76,472
  • 17
  • 159
  • 346
bel
  • 439
  • 1
  • 5
  • 17
  • When do you want doSomeStuff() to execute? – AbhinavRanjan Dec 21 '13 at 20:19
  • When I click on space. – bel Dec 21 '13 at 20:20
  • 2
    Thanks guys, I found answer in duplicate post - $('td > a').click(function(e) { e.stopPropagation(); }) – bel Dec 21 '13 at 20:28
  • Make sure it works on all browsers - I've experienced different behaviour from IE before, which required another way of disabling "bubbling". You can read up on it under _Turning it off_ here: http://www.quirksmode.org/js/events_order.html – Jervelund Dec 21 '13 at 20:44

2 Answers2

16

If you're using plain javascript. Try this

JS

function doOtherStuff(event,arg) {
        event.stopPropagation();
}

HTML

<tr>
     <td onClick="doSomeStuff();">
            <a href="#" onClick="doOtherStuff(event,1);">1</a>
            <a href="#" onClick="doOtherStuff(event,2);">1</a>
     </td>
</tr>
Thant Sin Aung
  • 682
  • 2
  • 10
  • 20
0

By using JQuery

<tr>
 <td>
    <span class="td">
      welcome
      <a href="#" class="a">1</a>
      <a href="#" class="a">2</a>
    </span>
 </td>
</tr>

JQuery Code

$(function(){
    $(document).on("click",".td",function(){
        alert('td');
    });
    $(document).on("click",".td a",function(e){
        e.stopPropagation();
    });
    $(document).on("click",".a",function(){
        alert($(this).text());
    });
});
abe
  • 624
  • 8
  • 14