-1

I need a little help with this function:

function passar() { 
 var fotos1 = document.getElementById("foto1");
 var fotos2 = document.getElementById("foto2");
 var fotos3 = document.getElementById("foto3");
 if ( fotos1.style.display = "block" ) { 
 fotos1.style.display = "none"; 
 fotos2.style.display = "block"; 
 } else if ( fotos2.style.display = "block" ) { 
 fotos2.style.display = "none"; 
 } 
} 

It was supposed to hide "fotos 2" when the first condition is changed but it didn't work, someone knows why?

3
  • 1
    i'm kind of upset that not a single person has said to use the identity operator === ... Commented May 24, 2012 at 22:22
  • 1
    == and != is one of the main reasons that the code quality of javascript is as low as it is... use === and !== instead. Commented May 24, 2012 at 22:23
  • I was the only one that gave the answer using === and no one cares. :) Commented May 24, 2012 at 22:24

3 Answers 3

2

= for assignment

== for comparison

=== for strict comparison

if (fotos1.style.display === "block") { 
fotos1.style.display = "none"; 
fotos2.style.display = "block"; 
 } else if (fotos1.style.display === "none") { 
 fotos2.style.display = "none"; 
 } 
} 
answered May 24, 2012 at 22:21
Sign up to request clarification or add additional context in comments.

1 Comment

Other languages have === too :-P
0

Your conditional statement is doing an assignment rather than a comparison:

if (fotos1.style.display === "block") { 
 fotos1.style.display = "none"; 
 fotos2.style.display = "block"; 
} else if (fotos1.style.display === "none") { 
 fotos2.style.display = "block"; 
}

Also, it is recommended to use strict comparison using === over the weak comparison ==. You get the added benefit of the comparison also being roughly 4X faster in some browsers.

answered May 24, 2012 at 22:21

1 Comment

Didn't realize it was that much faster. Cool.
-1

You should be using a comparison operator which is double equals in this case:

if (fotos1.style.display == "block") {

Instead you are using a single equals which is an assignment parameter:

if (fotos1.style.display = "block") {
Ayush
42.5k52 gold badges170 silver badges244 bronze badges
answered May 24, 2012 at 22:21

1 Comment

Can i know why was this given minus 1?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.