javascript - Different click event on 2 DIVs that are one in an other -


here code have tested:

div1 = document.createelement('div'); div1.setattribute('style','width:500px;height:500px;background-color:green;'); div1.addeventlistener('click',function()     {     alert('div1');     });  div2 = document.createelement('div'); div1.appendchild(div2); div2.setattribute('style','width:200px;height:200px;background-color:red;'); div2.addeventlistener('click',function()     {     alert('div2');     }); document.getelementsbytagname('body')[0].appendchild(div1); 

the problem is, need 1 function call, if click div inside. don´t wish function parent div, how possible make?

you need use event.stoppropagation().

div2.addeventlistener('click',function(event) {         event.stoppropagation();// prevents  event bubbling parents.         alert('div2'); }, false); 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -