0

So I'm fairly new to javascript, and I'm trying to use a simple if...else statement to toggle between showing and hiding a div element. My HTML page looks like this:

<!DOCTYPE html>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test</title>
<style type="text/css">
#fluffy {
            height:200px;
            width:200px;
            background-color:yellow;
            display:block;
}
#pepper {
            height:200px;
            width:200px;
            background-color:green;
            display:block;
}
</style>
</head>

<body>
<div id="fluffy"></div>
<div id="pepper" onclick="changeState()"></div>
<script type="text/javascript">
function changeState() {
var divState = document.getElementById('fluffy').style.display;
if (document.getElementById('fluffy').style.display == 'block') {
    document.getElementById('fluffy').style.display = 'none';
}
else if (document.getElementById('fluffy').style.display == 'none') {
    document.getElementById('fluffy').style.display = 'block';
} else {
    alert('test error message');
}
}
</script>
</body>

</html>

When I load the page in my browser, I receive an alert box containing 'test error message'. My original code had document.getElementById('fluffy').style.display stored in a variable called divState, but this didn't even give me an alert box, it didn't do anything. I get the feeling I'm using == wrong or something, but I'm really not sure. I've used ===, and when that didn't work I switched to ==, which is in the code above.

If anyone knows what exactly I'm doing wrong, I would appreciate the help.

Thanks, Harold

1
  • Do you use Firebug? It is an useable plugin for your browser, which shows you a bug fast always. You can browse all properties or check if some method exists etc. == is the right way to compare strings, I mean. I have similar troubles with storing objects to variables in Javascript. Sometimes, it is the best way dont use it. Commented Jul 31, 2013 at 14:35

6 Answers 6

1

Alright, it looks like you guys fixed my problems. I can't thank you enough, and I will definitely look into jQuery!

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

1 Comment

Welcome to StackOverflow and don't forget to mark the most helpful answer as Accepted :)
0

Try changing the onclick to be this

<div id="pepper" onclick="changeState"></div>

Comments

0

document.getElementById('fluffy').style.display is ''. Since you're setting styles with a stylesheet, you'll have to use getComputedStyle (plus friends for cross-browser compatibility). You can find an example cross-browser implementation in the answer to Get all computed style of an element.

Comments

0

I know, you're trying to learn JavaScript what I also want to encourage, but with jQuery, this whole stuff would be a one-liner plus crossbrowser-friendly etc.

<div id="fluffy"></div>
<div id="pepper"></div>

The <script> contains just:

$("#pepper").click(function () { $("#fluffy").toggle(); });

Try it out at JSFiddle.

2 Comments

But jQuery is not tagged here. Maybe he wants to try using pure JS.
@user1671639 sure, but well what's wrong with showing people some alternative solutions, especially if it looks that they attempt to reinvent the square wheel? ;)
0

When the page first loads, the div doesn't have any inline styles. element.style reads inline styles only.

You will need to render the div with style="display:block;" or if you can't/don't want to do that, look into getComputedStyle options for your supported browsers

3 Comments

But document.getElementById('fluffy').style.display; will return the respective right. So he can have it var divState = document.getElementById('fluffy').style.display; if ( divState == 'block') { This should work right?
@user1671639 No, as i said above, .style only uses inline styles that is the style="" attribute on the element. The OP can only use this method if display:block; is explicitly set on the element before this JS runs
Oh, I understood. OP's code will work only with inline style not in embedded or external styles. Thanks for sharing it.
0

Use computed style:

<div id="fluffy"></div>
<div id="pepper" onclick="changeState()"></div>
<script type="text/javascript">
function changeState() {
    var fluffy = document.getElementById('fluffy');
    var divState = window.getComputedStyle(fluffy).display;

    if (divState == 'block') {
       fluffy.style.display = 'none';
    }
    else if (divState == 'none') {
       fluffy.style.display = 'block';
    } else {
       alert('test error message');
    }
}
</script>

jsFiddle

1 Comment

May I suggest using document.currentStyle || window.getComputedStyle(), because older versions of IE don't support window.getComputedStyle() stackoverflow.com/questions/2664045/…

Your Answer

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