I’m filling this list:
<ul id="FolderList"></ul>
with a list of folders using jquery that produces the following HTML:
<ul id="FolderList">
<li id="FolderList0" onclick="return myFunc(0)">Item 1</li>
<li id="FolderList1" onclick="return myFunc(1)">Item 2</li>
<li id="FolderList2" onclick="return myFunc(2)">Item 3
<ul>
<li id="FolderList2a" onclick="return myFunc(2a)">Sub Item 1</li>
<li id="FolderList2b" onclick="return myFunc(2b)">Sub Item 2
<ul>
<li id="FolderList2bi" onclick="return myFunc(2bi)">Subsub Item 1</li>
</ul>
</li>
</ul>
</li>
</ul>
…
function myFunc(id) {
//do something
return false;
};
For some reason if i click on a level 1 li item, the function myFunc() executes as expected. If i click on a “level 2” item (ie: FolderList2a), myFunc is being called twice. If i click on a 3rd level (ie: FolderList2bi) it gets called 3 times – and so on.
Anyone know what’s going on here?!
Thanks in advance!
The click events are bubbling up the Dom
If you want to prevent bubbling letmyFuncreturn falseTo stop the bubbling you’ll need to access the event object,
event.stopPropagationorevent.cancelBubbledepending on browser.http://jsfiddle.net/Jetyc/3/