Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
11
rated 0 times [  12] [ 1]  / answers: 1 / hits: 26636  / 15 Years ago, wed, september 9, 2009, 12:00:00

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


More From » jquery

 Answers
67

You can add a handler for the child that will prevent the click event from propagating up:



function handler(event) {
event.stopPropagation();
// now do your stuff
}
$('#a').add('#b').click(handler);


This way clicks to '#b' will not propagate to '#a'. Neither will clicks to '#c' go to '#b', and hence not to '#a'.


[#98730] Friday, September 4, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jennie

Total Points: 593
Total Questions: 102
Total Answers: 106

Location: Federated States of Micronesia
Member since Fri, Sep 16, 2022
2 Years ago
jennie questions
;