I have three div boxes
The first div main has an id of "main"
and contains two other boxes with id "menu_header"
and "menu_options"
this is my html
<div id="main">
<div id="menu_header">
</div>
<div id="menu_options">
</div>
</div>
<span>Something here</span>
this is my css
#main{
display:table-cell;
position:relative;
top:100px;
border:solid purple;
}
#menu_header{
position:relative;
border:solid black;
background:red;
width:100px;
top:200px;
height:100px;
/* -moz-transition-property:bottom;
-moz-transition-duration:2s;
transition-property:top;
transition-duration:1s;*/
}
#menu_options{
background:blue;
position:absolute;
bottom:0%;
border:solid black;
width:100px;
height:100px;
}
/*#menu_header:active{
top:100px;
}*/
(ignore css transition since it was commented out)
this is my javascript
function test(e){
//if(e.target.id=="menu_options"){return};
$('#menu_header').animate({top:'-10'},1000);
//var t=document.getElementById('menu_header')
// t.setAttribute('top','40px');
t.style="position:relative;border:solid black;background:red;width:100px;top:60px;height:100px;"
}
document.getElementById('main').addEventListener('click',test,false)
finally this is my lab rat jsfiddle link http://jsfiddle.net/repzeroworld/ev7k688s/
In this scenario, when I set a click event handler on my parent container with id "main"
everything works perfectly except that whenever click on this parent's container child div with id "menu_options"
it also triggers the handler
I found the link below but somehow the event.propogation()
solution here seems not to work in my case.
clicking on child element also triggers click event on it parent
One nasty hack, to prevent the any actions from executing if the child div capture the event was
if(e.target.id=="menu_header"){return};
THe above I commented out in my javascript code.
Question:
why is the child div with id "menu_options"
capturing the event from the parent container with id "main"
despite me setting false
on add EventListener
to prevent event bubbling.?
event.stopPropagation()
, notevent.propagation()
. Settingfalse
withaddEventListener()
does not disable bubbling. – user1106925