Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

198
Vistas
Why html will not keep css changes made by javascript when I click a button

I asked this question to answer it because there is no clear info or question about this. I found the solution in a comment, not in an answer. so I hope this will help others.

HTML

<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  
  <a href="" class="btn" id="btn" onclick="clickBtn()">btn</a>
  
  
  <div class="box" id="box"></div>
  
 
  
</body>
</html>

CSS

.btn{
  width: 150px;
  height: 50px;
  cursor: pointer;
  margin: 10px;
  background: black;
  color: white;
}

.box{
  width: 200px;
  height: 200px;
  background: orange;
  margin: 5px;
}

javascript

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

function clickBtn() {
   
  if (box.style.background = "orange"){ 
      box.style.background = "blue";
  } else {
      box.style.background = "green";
  }
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

1- If you use <a> as a button, it will refresh the page as long as it has href="". so remove href and it will work without refreshing the page.

2- if you want to keep the href, then change the <a> to button. it worked for me.

about 4 years ago · Juan Pablo Isaza Denunciar

0

use href="javascript:void(0)" in a tag. The javascript:void(0) can be used when we don't want to refresh or load a new page in the browser on clicking a hyperlink.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda