I am a newbie to Javascript and I have found this code for collapsible panels which works great, however i would like the panel to begin collapsed rather then expanded when the page is loaded. Can anyone help please? thank you in advance.
However, now on initial load up the panel requires 2 clicks to expand it. once it has been initially expanded one click closes it and opens it. why does it need 2 clicks initially and is there a way to fix it?
I put the display: none in the css block at the top of the page. Its quite amazing that putting it there causes you to have to click twice but putting it in the main block corrects it. Why does that happen?
Because the element.style object relates to the inline styles. There’s a completely separate and not very cross-compatible way to access the computed styles, and the stylesheets themself. Quite frankly it’s a mess.
We can work around it though by adjusting what the script looks for.
The elements style could be “” or “none” when it’s closed.
It can only be “block” when it’s open, so we can check for that instead, and use ! to invert the result.
Oh yes, I forgot to add that having it closed by default from the CSS stylesheet guarantees that anyone who is not using JavaScript won’t be able to see it.
For such reasons, it’s often better to use scripting to close such elements instead, which also neatly resolves the trouble with the element.style object.
Okay, this requires running some script after the page content is available. It’s not possible to run the script directly from the head, because at that particular time nothing below it exists yet.
It’s possible to use window.onload or other domcontentready events, but this is where we can make good use of the best-practice technique where you put scripts at the bottom of the page. Just before the </body> tag.
First we need scripting to obtain a reference to the element. That can easily be done if there’s a unique identifier, but with a class name things get a bit trickier.
Some web browsers natively support getElementsByClassName while others don’t. One technique is to provide a completely separate getElementsByClassName function, and a simpler technique is to loop through your DIVs looking for one who’s class name matches what we’re after.
I’ll use the simpler technique here since there’s no point in adding three times the scripting code if it’s only going to be used just the once.
var divs = document.getElementsByTagName('div'),
divsLen = divs.length,
div,
i;
for (i = 0; i < divsLen; i += 1) {
if (divs[i].className === 'squareboxcontent') {
div = divs[i];
break;
}
}
div.style.display = 'none';
Remove that added css style, put in the above script at the end of your body (a script reference to an external script file is preferred, instead of inline scripting), just before the </body> tag, and you’ll be all good to go.
it works perfectly when javascript is turned off, it displays the expanded content. However, when javascript is turned back on it has the first one collapsed but the second and third and expanded?
Okay, when dealing with multiple elements things have to be done a little bit differently.
Here’s an update to the script.
var divs = document.getElementsByTagName('div'),
divsLen = divs.length,
div,
i;
for (i = 0; i < divsLen; i += 1) {
div = divs[i];
if (div.className === 'squareboxcontent') {
div.style.display = 'none';
}
}