Forum Moderators: open

Message Too Old, No Replies

Need to modify expand/collapse <div> script

         

jomoweb

9:34 pm on Apr 17, 2006 (gmt 0)

10+ Year Member



I am trying to modify the onload collapseportion of an expand/collapse <div> script. The script works great, except it is meant to collapse multiple <div>'s on page load. Well, I only have 1 div that I need to collapse. It works, but brings up the "Error on Page" icon in IE. I have determined that the error is due to the script wanting multiple divs, but I only have the one. I want to figure out how to modify this script so that it works with only 1 div, instead of 3. (code line in bold is the one that triggers the error)

JavaScript:


function collapseAll(objs) {
var i;
for (i=0;i<objs.length;i++ ) {

objs[i].style.display = 'none';

}
}
function pageLoad() {
collapseAll($('myDiv1','myDiv2','myDiv3'));
}
addEvent(window,'load',pageLoad);

HTML:


<a onclick="switchMenu('myDiv1');">Expand/Collapse</a>
<div id="myDiv1">
Material that is to be expanded/collapsed.
</div>

jomoweb

9:48 pm on Apr 17, 2006 (gmt 0)

10+ Year Member



For a workaround, I tweaked the last line of the script like this:

[b]collapseAll($('myDiv1','myDiv1'));[b]

However, there is probably a better way!?

fintan

1:31 pm on Apr 18, 2006 (gmt 0)

10+ Year Member



You could try something like this.

function ShowTab(tab,id){ // tab is an array, id is the div's name/id.

var array_length = tab.length; // Get's the length of the array

for(var i=0; i<array_length; i++){ // Loops through the array
var obj = document.getElementById(tab[i]).style; // Shorthand for the div's style

if(tab[i] == id){ // Checks to see which div is selected and sets the visibility
obj.visibility = 'visible';
}
else{
obj.visibility = 'hidden';
}

}
}