Skip to content
Advertisement

Make Collapsible Divs NOT hidden by default

I am using the Collapsible divs code from here: https://www.w3schools.com/howto/howto_js_collapsible.asp

The collapsible divs are hidden by default, but I can’t figure out how to edit the JavaScript to make it so they are all visible by default & only hidden when you click the button to hide them.

Code from Example:

HTML

JavaScript

CSS

JavaScript

Java-Script

JavaScript

Any help would be much appreciated. Thanks! – Lauren

Advertisement

Answer

You do have to change the css of .content to display: block; but also, you’ll have to change the JavaScript so that when you click on the open collapsible it closes. Otherwise, you’ll have to click on it twice for it to close, as on the first click it still doesn’t have any content.style.display.

In particular this check here:

JavaScript

See the snippet below:

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement