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

399
Vistas
how to make an element still active be kept active even after refresh?

I'm using this code to active Current Element. I found this code on "w3schools.com". in this code, If I refresh, the currently active element disappears. How can the element that is still active be kept active even after refresh?.

// Add active class to the current button (highlight it)
var header = document.getElementById("myDIV");
var btns = header.getElementsByClassName("btn");
for (var i = 0; i < btns.length; i++) {
  btns[i].addEventListener("click", function() {
    var current = document.getElementsByClassName("active");
    current[0].className = current[0].className.replace(" active", "");
    this.className += " active";
  });
}
/* Style the buttons */

.btn {
  border: none;
  outline: none;
  padding: 10px 16px;
  background-color: #f1f1f1;
  cursor: pointer;
  font-size: 18px;
}


/* Style the active class, and buttons on mouse-over */

.active,
.btn:hover {
  background-color: #666;
  color: white;
}
<h1>Active Button</h1>
<p>Highlight the active/current (pressed) button.</p>

<div id="myDIV">
  <button class="btn">1</button>
  <button class="btn active">2</button>
  <button class="btn">3</button>
  <button class="btn">4</button>
  <button class="btn">5</button>
</div>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I solved your problem, try this

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
/* Style the buttons */
.btn {
border: none;
outline: none;
padding: 10px 16px;
background-color: #f1f1f1;
cursor: pointer;
font-size: 18px;
}

/* Style the active class, and buttons on mouse-over */
.active, .btn:hover {
background-color: #666;
color: white;
}
</style>
</head>
<body>

<h1>Active Button</h1>
<p>Highlight the active/current (pressed) button.</p>

<div id="myDIV">
<button class="btn">1</button>
<button class="btn active">2</button>
<button class="btn">3</button>
<button class="btn">4</button>
<button class="btn">5</button>
</div>

<script>


 var header = document.getElementById("myDIV");
 var btns = header.getElementsByClassName("btn");

for (var i = 0; i < btns.length; i++) {
     btns[i].addEventListener('click', function(e){
        var current = document.getElementsByClassName("active");
        current[0].classList.remove('active');
        this.classList.add('active');
        var array = [];
        array.push(btns);
        localStorage.setItem('element', this.innerText);
     });
}

    function setActivatedItem(){
        var item = localStorage.getItem('element');

        if(item){
            for (var a = 0; a < btns.length; a++) {
                if(btns[a].innerText == item){
                    btns[a].classList.add('active');
                }else{
                    btns[a].classList.remove('active');
                }
            }
        }
    }

    window.onload == setActivatedItem();

</script>

</body>
</html>
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