Well, it's checking the default image (collapse.gif) to see if it's there (which it will be when the page is first loaded). So, if the user has scripts enabled, it changes to the image to expand.gif and collapses all of the divs - so that the expandable tree is in a fully collapsed state to start. But if the user is does not have scripts enabled, nothing happens and the collapse.gif remains with the fully expanded tree.
I have a separate function that does the following on click (not on load):
1) toggles the image from expand.gif to collapse.gif (and vice versa),
2) expands/collapses the div, and
3) jumps to the div.
Um... I got the classname.match method from an example somewhere. I think I used it because I needed the function to check for all existing instances of the image and cycle through them. Your way looks simpler if it does the same thing, but I had tried various methods and the one I used worked so I left well enough alone. :) Plus I remember reading something about IE 7 having issues with getElementsByCl assName, though I suppose it may have the same issues with mine. <shrug> I have no idea.
HERE'S ALL OF THE CODE WITH COMMENTS IN CASE ANYONE HAS USE OF IT. SORRY IF IT'S NOT THE MOST EFFICIENT CODE, BUT IT WORKED FOR ME:
This is in my head tags:
This is how I used it in various places in the page:
Hope this helps someone.
UberGirl
I have a separate function that does the following on click (not on load):
1) toggles the image from expand.gif to collapse.gif (and vice versa),
2) expands/collapses the div, and
3) jumps to the div.
Um... I got the classname.match method from an example somewhere. I think I used it because I needed the function to check for all existing instances of the image and cycle through them. Your way looks simpler if it does the same thing, but I had tried various methods and the one I used worked so I left well enough alone. :) Plus I remember reading something about IE 7 having issues with getElementsByCl assName, though I suppose it may have the same issues with mine. <shrug> I have no idea.
HERE'S ALL OF THE CODE WITH COMMENTS IN CASE ANYONE HAS USE OF IT. SORRY IF IT'S NOT THE MOST EFFICIENT CODE, BUT IT WORKED FOR ME:
This is in my head tags:
Code:
<script language="javascript">
<!-- The following 5 functions all are in place to collapse the divs when a user has javascript enabled. -->
<!-- This function is called onload and calls toggledisplay for all divs with class 'ac' -->
function HideDiv1() {
var boxes = document.getElementsByTagName("div");
for(var i=0;i<boxes.length;i++)
if(boxes[i].className.match("ac")) toggledisplay(boxes[i]);
}
<!-- This function is called onload and calls toggledisplay for all divs with class 'tn'. I didn't know how to do both classes in the same function because I'm a newbie. Probably easy with a comma somewhere. But the second class (tn) is indented more than the first. -->
function HideDiv2() {
var boxes = document.getElementsByTagName("div");
for(var i=0;i<boxes.length;i++)
if(boxes[i].className.match("tn")) toggledisplay(boxes[i]);
}
<!-- This function is called onload and calls toggleimage for all images with class 'ti'. -->
function ChangeImage() {
var boxes = document.getElementsByTagName("img");
for(var i=0;i<boxes.length;i++)
if(boxes[i].className.match("ti")) toggleimage(boxes[i]);
}
<!-- This function hides the divs found in HideDiv1 and HideDiv2. -->
function toggledisplay(elem) {
visibility = elem.style;
if(visibility.display=='block')
visibility.display = 'none';
}
<!-- This function changes the image from minus sign (collapse.gif) to plus sign (expand.gif) for all images found in ChangeImage. -->
function toggleimage(elem) {
if ('http://www.yourdomainhere.com/images/collapse.gif' == elem.src) // Note: I had to include the entire URL of the image, including the domain, to get this to work.
var image = 'http://www.yourdomainhere.com/images/expand.gif';
elem.src = image;
}
<!-- This function is not called onload. It's called onclick. It does three things: 1) Hides/shows the div, 2) toggles the plus/minus image, and 3) jumps to the div when showing it (expanding). -->
function showHide(divID, imgID){
var JUMPDIV = new String("#_" + divID);
if (document.getElementById(divID).style.display == "none") {
document.getElementById(divID).style.display = "block";
document.getElementById(imgID).src = "images/collapse.gif";
window.location.hash = JUMPDIV;
} else {
document.getElementById(divID).style.display = "none";
document.getElementById(imgID).src = "images/expand.gif";
}
}
</script>
This is how I used it in various places in the page:
Code:
<body onload="HideDiv1(); HideDiv2(); ChangeImage();">
<a name="_div1"></a>
<div class="class1"><img id="image1" class="ti" src="images/collapse.gif" onclick="showHide('div1',this.id);" /> DIV ONE</div>
<div id="div1" class="ac" style="display:block">
<ul>
<li><a name="_subdiv1"></a><img id="subimage1" class="ti" src="images/collapse.gif" onclick="showHide('subdiv1',this.id);" align="absmiddle" width="14" height="14" /> SUB DIV ONE
<div class="tn" id="subdiv1" style="display:block"> SUB DIV ONE INFO </div>
</li>
</ul>
</div>
Hope this helps someone.
UberGirl
Comment