Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
Haga clic en un div para cambiar el color de la sombra, vuelva a hacer clic para volver a cambiar

¿Es posible hacer esto sin usar JQuery?

Me gustaría que un div cambiara el color de su sombra una vez que hice clic en él, y cuando vuelva a hacer clic en él, volverá a cambiar a su color original. Aquí está el código: 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>

¡Gracias!

over 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

En este caso, es mejor alternar una clase usando 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>

over 4 years ago · Juan Pablo Isaza Report

0

Puede hacer uso de un booleano con una declaración if/else para este problema

 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; } }
over 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!