Prevent execution of parent event handler

Viewed 26229

I have a tree of divs:

<div id="a" onclick="func">
    <div id="b" onclick="func">
         <div id="c" onclick="func">
         </div>
    </div>
</div>

When a click is made on a div it makes it's children invisible - ie click on "a" will turn "b" and "c" invisible.

function func{
   if ($(childId).hasClass("visible")){
    $(childId).removeClass("visible");
    $(childId).addClass("invisible");
}

The problem is: a click on "b" will call "a"'s click and make "b" and "c" invisible. How do I disable the click on "a" using jQuery?

thanks

3 Answers

I have the following htmlstructure:

<a href="http://example.com">
    <div> 
       <button type="button">Save</button>
    </div>
</a>

and I want to prevent new page being opened when button is clicked.

I found that stopPropagation alone is not enough. The handler should also return false.

$('button').click(function(event){
        event.stopPropagation();
        save();
        return false;
});
Related