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 Answers 3
= 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";
}
}
1 Comment
=== too :-PYour 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.
1 Comment
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") {
===...==and!=is one of the main reasons that the code quality of javascript is as low as it is... use===and!==instead.===and no one cares. :)