Hello Friends,
I am developing a menu using javascript wherein, I expand and collapse divs. It works fine individually; but the problem arises when I try to hide all other divs on expanding one div.
Following is the code. Pls. suggest any change if you spot any error or even any alternative way to solve this problem.
Thank You.
<head>
<script>
function hideDivs(){
var arr = document.getElementsByTagName('div')
for(var i=0; i<arr.length;i++){
arr[i].style.display = (arr[i].style.display == 'block')? 'none':'block';
}
}
function hideOtherDivs(divID){
var arr = document.getElementsByTagName('div')
for(var i=0; i<arr.length;i++){
if(arr[i].id!=divID){
arr[i].style.display = 'none';
}
else
document.getElementById(divID).style.display=(document.getElementById(divID).style.display!="block")? 'none' : 'block';
}
}
</script>
</HEAD>
<BODY onload = "hideDivs();return false;">
<a href=# onClick="hideOtherDivs('div1')">Title 1</a>
<div id="div1" style="display:block;">
Sub-menu1</div>
<a href=# onClick="hideOtherDivs('div2')">Title 2</a>
<div id="div2" style="display:block;">
Sub-menu2</div>
</body>
The function hideOtherDivs() hides all the divs.
Instead if I use follwing line, it atleast shows the sub-menu.
<a href="#" onclick="document.getElementById('div1').style.display=(document.getElementById('div1').style.display== 'block')?'none':'block';">