0

I need help collapsing a collapsible div on page load.

I'm using this JavaScript code:

<script type="text/javascript">
    function switchMenu(obj) {
        var el = document.getElementById(obj);
        if ( el.style.display != "none" ) {
            el.style.display = 'none';
        }
        else {
            el.style.display = '';
        }
    }
    document.getElementById('aboutme').style.display = 'none';
</script>

to collapse HTML div id="aboutme" when the <a ...>about me</a> is clicked:

<div class="container">
    <a href="#" onclick="switchMenu('aboutme');">about me</a>
    <div id="aboutme">
        sample text to be expanded and collapsed
    </div>
</div>

I can't get the page to close my div#aboutme on page load.

I want this page to load with my div collapsed.

I thought that the JS line

document.getElementById('aboutme').style.display = 'none';

should do the trick but it doesn't. What am I doing wrong?

Thanks for your help.

2
  • 1
    just place the script after the html code Commented Jan 1, 2012 at 12:18
  • Can you post a live example of this not working. jsfiddle is a good place, it will be easier for us to help then. Commented Jan 1, 2012 at 12:27

2 Answers 2

4

If you want your div to load collapsed, simply write the following

 <div id="aboutme" style="display:none">
        sample text to be expanded and collapsed
    </div>

This should resolve the problem. However, if you are still interested in the JavaScript solution keep reading.
As you said I can't get the page to close my div#aboutme on page load - the problem is that you are not using "onload" event. Simply put the line document.getElementById('aboutme').style.display = 'none'; in your body's onload attribute.. something like this

<body onload="document.getElementById('aboutme').style.display = 'none';">
...
</body>

and you should see the results with JavaScript. I recommend you use "style" method instead. much better.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This html solution is exactly the simple method I was looking for.
1

Exactly how do you make that JS run on window load? It may simply run before the page is rendered

Does clicking on the link work? if it does, that would prove that the issue is simply the loading sequence

The easiest solution would be to place your code at the very end of your HTML file, just before the closing </body> tag. The code below is more generic, and can be placed anywhere. Note that to toggle the link back on I set the display to 'inline' (or block, i.e. whatever it was before - you may want to save that to a variable to be sure)

<script type="text/javascript">
function switchMenu(id) {
    var el = document.getElementById(id);
    if ( el.style.display != "none" ) {
        el.style.display = 'none';
    }
    else {
        el.style.display = 'inline'; //or block - i.e. whatever it is rendered by
    }
}

//add to the window onload event
if( window.addEventListener ){
    window.addEventListener( 'load', function(){ switchMenu('aboutme')}, false);
} else if (window.attachEvent) {
    window.attachEvent("onload", function(){ switchMenu('aboutme') } );
}
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.