4

Testing out a simple toggle display, however, it takes two clicks to toggle the display the first time. Afterwards it does it in one.

<html>
<head>
<style>
#carousel{border:2px solid blue;
width:1280px;
height:720px;}

#p2{visibility:hidden;}

#p1{display:block;}

#btn{position:absolute;
    top:2000px;}
</style>

<script src="mainScript.js"></script>
</head>

<body>

<div id="carousel">
    <img id="p1" src="pic1.jpg">
    <img id="p2" src="pic2.jpg">
</div>

<button type="button" id="button" onclick="clickEvent()">Click</button>

</body>
</html>

And here is my javascript:

function clickEvent(){
var p = document.getElementById("p1");
if(p.style.display == "block")
    p.style.display = "none";
else
    p.style.display = "block";
}

It should be noted I am using no jQuery, as all other questions I found about this were jQuery related.

2
  • 1
    Check out this fiddle I put together. I tested your code and just added the JavaScript in a script tag in the <head></head> tag of your html and it worked wonderfully for me. ic3b3rg's answer is a good simplification of your code but from the way jsfiddle looked, it appeared your code was working correctly as is. I tested it in Chrome. You might check that your reference <script src="mainScript.js"></script> is pointing to the right place. Commented Dec 31, 2012 at 16:30
  • Tried that, and it didn't work. And my mainScript.js is indeed in the right place. Tested it in several browsers, all do the same thing Commented Dec 31, 2012 at 16:37

5 Answers 5

5
function clickEvent(){
var p = document.getElementById("p1");
if(p.style.display == "none")
    p.style.display = "block";
else
    p.style.display = "none";
}

you can also simplify things a bit:

p.style.display = p.style.display == "none" ? "block" : "none";
Sign up to request clarification or add additional context in comments.

Comments

1

I have an update to my previous fiddle posted in my comment above. My previous fiddle still ran into the same problem after further testing of the double click.

After stepping through, the initial display value is coming back as "" not block. I'm not sure why its not taking your value you set in the <head></head> section but if you inline it like so:

<img id="p1" src="pic1.jpg" style="display: none;" />

it works correctly the first time with only one click of the button.

Here is my new updated fiddle demonstrating this.

I'm going to look more into why your styling in the <head></head> section but for now, here is a quick (and semi crude) fix.

Hope this helps and best of luck!

1 Comment

This fixed it, many thanks! All I did was change "block" to "inline". Everyone has been very helpful, and this was a wonderful first experience using Stack Overflow.
1

The default display attribute is "inline" so your logic is not taking this into account. It is changing it the block on the first run, so it is still visible, then it is hiding it on the second click (setting display to none)

4 Comments

The OP is setting the initial display value. I.E. he is overwriting inline with #p1{display:block;}
If you take a look at the CSS, I already set the p1 element display attribute to block. EDIT: Didn't see war10cks comment
p.style.display will actually return an empty string in Firefox the first time it is read.
So isn't the problem that the display property is block initially, making the image visible? The button works properly, toggling the image to be visible/invisible.
0

your answer

    function clickEvent(){
    var p = document.getElementById("p1");
    if(p.style.display === "block")
        p.style.display = "none";
    else
        p.style.display = "block";
    }

I changed the condition I have the same problem and I realize that it was about the order code executes my CSS style for the element was already "block", and I was checking if the element display was "none" then do the display block thing, so when the first time I was clicking, it changed the display to "none", then in the second time it would change the display to block, I hope It was clear my explanation enjoy

1 Comment

its about order in your style
-2

The same problem can be resolved by just replacing "block" : "none"; by ?"none" : "block"; you will not need to double click the toggle button for the first time, single click will work.

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.