10

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?

4
  • When do you want doSomeStuff() to execute? Commented Dec 21, 2013 at 20:19
  • When I click on <td> space. Commented Dec 21, 2013 at 20:20
  • 2
    Thanks guys, I found answer in duplicate post - $('td > a').click(function(e) { e.stopPropagation(); }) Commented Dec 21, 2013 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: quirksmode.org/js/events_order.html Commented Dec 21, 2013 at 20:44

2 Answers 2

20

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>
Sign up to request clarification or add additional context in comments.

1 Comment

what is event inside html?
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());
    });
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.