Hopefully this will be an easy fix for someone.
I'm trying to implement the Hide/Show toggle at https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_toggle_hide_show
The script part of it is
<script>
function myFunction() {
var x = document.getElementById("myDIV");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
My question is, the page always starts with the item being shown, not hidden. How do I make it when the page is first loaded the item is hidden, not shown?
Thanks for any help with this.
Basically you just need to set #myDIV to display: none since the script you already have checks if display is none and if it is it sets it to block. If display is something other than none it sets it to none. So it doesn't matter what it is to start with for the script to work.
But it's bad practice to hide a toggle object by default because users without JS can't make it visible. For that reason it's better to let JS hide it when the page loads. That way it will always be visible if JS isn't available. The whole script block would look something like this.
Thank you pandy! I appreciate it.
That worked perfectly.
Good.
Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)