OK, a quick fix first, although this is not the best way to use JS on your page:
Change the call of this:
<div onclick="test(this);" class="test">
Then in the test use this:
function test(el){ var uls = el.getElementsByTagName('ul'); for(var i = 0; i < uls.length; i++){ if(uls[i].className == 'innermenu'){ uls[i].style.display = "none"; break; } } }
This will hide only the ul div child that will be clicked.
The best way
OK, for a longer answer. Either attach events after the fact using attachEvent and addEventListener , or use the jQuery library to help you. Here is the original solution:
Customize your HTML in this way (no onclick ):
<div class="test">
And then at the very end of your HTML, put this:
<script type="text/javascript"> var divs = document.getElementsByTagName('div'); function test(){ var uls = this.getElementsByTagName('ul'); for(var i = 0; i < uls.length; i++){ if(uls[i].className == 'innermenu'){ uls[i].style.display = "none"; break; } } }; for(var i = 0; i < divs.length; i++){ var div = divs[i]; if(div.className !== "test") continue; if(window.addEventListener){ div.addEventListener( 'click', test, true ); </script>
Now you do not have JavaScript code in your HTML, and you can get rid of the additional parameter in the test function.
source share