Reputation: 17
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?
Upvotes: -1
Views: 99
Reputation: 500
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") {
Upvotes: -1
Reputation: 4021
=
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";
}
}
Upvotes: 2
Reputation: 17815
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.
Upvotes: 0