Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

152
Visualizações
Javascript Equals to gets highlighted in if statement

So i was making a pretty simple carousel in which when a user clicks on a button, the image or content changes. I got it to work halfway but i am only one problem. In the code i first check if the display of card1 is block and the display of card3 is none. That is where it shows the problem. In the first part it is fine but after the && when i write card3.style = "display:none" it highlights the equals to sign and doesn't work. I have tried changing the orientation of the code and even adding ==but to no avail. Can someone help me with this?

You can checkout my code:

   <div class="card1" id="card1">
        
    </div>

    <div class="card2" id="card2">
        
    </div>

    <div class="card3" id="card3">
        
    </div>

    <button onclick="change()">Change</button>
.card1{
    width: 40%;
    height: 40%;
    background-color: blue;
}
.card2{
    width: 40%;
    height: 40%;
    background-color: red;
    display: none;
}
.card3{
    display: none;
    width: 40%;
    height: 40%;
    background-color: green;
}
var card1 = document.getElementById("card1")
var card2 = document.getElementById("card2")
var card3 = document.getElementById("card3")

function change(){
    if(card1.style = 'display:block;' && card3.style = 'display:none'){
        card1.style = 'display:none';
        card2.style = 'display:block';
    
    }
    

}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Use == to compare:

var card1 = document.getElementById("card1")
var card2 = document.getElementById("card2")
var card3 = document.getElementById("card3")

function change(){
    if(card1.style.display == 'block' && card3.style.display == 'none'){
        card1.style = 'display:none';
        card2.style = 'display:block';
    
    }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Use == operator and update card1.style to card1.style.display.

var card1 = document.getElementById("card1");
var card2 = document.getElementById("card2");
var card3 = document.getElementById("card3");

function change() {
  if (card1.style.display == 'block' && card3.style.display == 'none') {
    card1.style = 'display:none';
    card2.style = 'display:block';
  }
}
.card1 {
  width: 40%;
  height: 40%;
  background-color: blue;
}

.card2 {
  width: 40%;
  height: 40%;
  background-color: red;
}

.card3 {
  display: none;
  width: 40%;
  height: 40%;
  background-color: green;
}
<div class="card1" id="card1" style="display: block;">
  Card 1
</div>

<div class="card2" id="card2" style="display: none;">
  Card 2
</div>

<div class="card3" id="card3" style="display: none;">
  Card 3
</div>

<button onclick="change()">Change</button>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda