i have a div
<div id="mainDiv" onclick="devide(this.id, event);" style="z-index:1;position:relative; width:400px; height:400px; border:1px red solid;"></div>
on its click i append to more divs to it and want the same thing to happen to them, means on there click 2 more divs shud b added to them
on click on mainDiv
mainDiv
-mainDiv0
-mainDiv1
on click on mainDiv0
mainDiv
-mainDiv0
-mainDiv00
-mainDiv01
-mainDiv1
i guess i was able to clear the things
i used following js
function devide(id, evt){
alert(id);
var divElement = document.getElementById(id);
if(typeof(divElement) != "undefined" && divElement!=null){
captureMousePosition(evt);
if(splitVertical==1)
verticalSplit(divElement);
else
horizontalSplit(divElement);
}
}
function verticalSplit(divElement){
divElement.style.border = "0px red dashed";
// divElement.removeAttribute("onclick");
var width = ((divElement.offsetWidth-(xMousePos-LEFT_MARGIN))/divElement.offsetWidth)*100;
var newDiv1 = document.createElement("div");
newDiv1.setAttribute("id", divElement.id+"0");
// newDiv1.onclick = "devide('"+divElement.id+"0', event);";
newDiv1.style.width = (100-width)+"%";
newDiv1.style.height = "100%";
newDiv1.style.float = "left";
newDiv1.style.border = "1px red dashed";
newDiv1.style.zIndex = divElement.style.zIndex+1;
var newDiv2 = document.createElement("div");
newDiv2.setAttribute("id", divElement.id+"1");
newDiv2.style.width = width+"%";
newDiv2.style.height = "100%";
newDiv2.style.float = "left";
newDiv2.style.border = "1px red dashed";
newDiv2.style.zIndex = divElement.style.zIndex+1;
divElement.appendChild(newDiv1);
divElement.appendChild(newDiv2);
newDiv1.addEventListener("click",devide(divElement.id+"0", event),true);
newDiv2.addEventListener("click",devide(divElement.id+"1", event),true);
}
but this is recursively calling devide function for the first encountered element say mainDiv0,mainDiv00,mainDiv000…. and so on
please help me with this
The situation you are describing is called event bubbling.
The W3C model says you can call
.stopPropagation()on the event. IE might needwindow.event.cancelBubbleTo do unobtrusive javascript with your code try:
HTML
JS