Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you toggle the visibility of a div in FF with javascript? (IE and Chrome work fine)

So, I'm trying to toggle the visibility of a div layer using javascript and this is what it looks like:

document.all["layer1"].style.visibility='visible';

and

document.all["layer1"].style.visibility='hidden';

What would the syntax look like for this if I'm trying to do it in Firefox? If I know that, then I'll add in an if statement to check if the browser is firefox and use the alternate code.

like image 852
Alex Datcu Avatar asked Oct 20 '25 14:10

Alex Datcu


2 Answers

document.all is a not a supported function, nor is it in the spec for the DOM (Here is more on that). You will have to grab the div by another method.

querySelector

document.querySelector('div#layer1').style.visibility = 'hidden';    
document.querySelector('div#layer1').style.visibility = 'visible';

OR

getElemenyById

document.getElementById('layer1').style.visibility = 'hidden';
document.getElementById('layer1').style.visibility = 'visible';

are two major methods.

like image 64
zellio Avatar answered Oct 22 '25 04:10

zellio


Don't use document.all[]. It is not supported by all browsers, and is largely an artifact of the IE4 days. Instead use document.getElementById() to access the <div>'s id attribute:

document.getElementById("layer1").style.visibility = 'hidden';
document.getElementById("layer1").style.visibility = 'visible';
like image 26
Michael Berkowski Avatar answered Oct 22 '25 04:10

Michael Berkowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!