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

222
Visualizações
Click a div to change the shadow color, re-click to change back

Is it possible to do this without using JQuery?

I would like a div to change its shadow color once I clicked on it, and when I click on it again it will change back to its original color. Here's the code: http://jsfiddle.net/kqv7x5f8/5/

var btn = document.getElementById("btn");

function toShadow() {
  document.getElementById("btn").style.boxShadow = "5px 5px red"
}
#btn {
  height: 20px;
  border: 2px solid black;
  box-shadow: 5px 5px black;
}
<div id="btn" onclick="toShadow">
  click to change shadow; click again to change back
</div>

Thank you!

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

0

It is better in this case to toggle a class by using classList.toggle()

const button = document.getElementById('btn');

button.addEventListener('click', e => e.currentTarget.classList.toggle('shadow'));
#btn {
  height: 20px;
  border: 2px solid black;
}

.shadow {
  box-shadow: 5px 5px black;
}
<div id="btn">
  click to change shadow; click again to change back
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

You can make use of a boolean with an if/else statement for this problem

var btn = document.getElementById("btn");
var isRed = false;

function toShadow(){
if(isRed){
btn.style.boxShadow ="5px 5px black"
isRed = false;
}
else {
btn.style.boxShadow ="5px 5px red"
isRed = true;
}
}
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