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

189
Vistas
Add/remove style after click label/input[radio] problem

I have a problem with a function that selects active items. There are some input/label fields with sectoins ( one section - one active/clicked element ) It only works when I double-click. If I click one time then no result. I can set setTimeout for that code and then works but not always.

Example:

var radios = document.querySelectorAll('#calculator input[type=radio]'); 
var label = document.querySelectorAll("#calculator label"); 

function active(e) {

    radios.forEach((el,index) => {
        if(el.checked === true) {
            console.log('true');
            label[index].classList.add('active');
        } else {
            console.log('false');
            label[index].classList.remove('active');
        }
    })

}

Example html field:

<input id="p75" type="radio" name="geodesic_size" value="11250" >
<label for="p75">
<input id="p30" type="radio" name="geodesic_size" value="5626" checked>
<label for="p30">
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Not sure if this is what you need, but here's an example of toggling the active class when the checked input changes:

const radios = document.querySelectorAll('input[type="radio"]');
const label = document.querySelectorAll("label"); 

function active(e) {
  radios.forEach((el,index) => {
    if(el.checked === true) {
      console.log('true');
      label[index].classList.add('active');
    } else {
      console.log('false');
      label[index].classList.remove('active');
    }
  })
}

radios.forEach(el => {
  el.addEventListener('change', active);
})
.active {
  color: red;
}
<input id="p75" type="radio" name="geodesic_size" value="11250" >
<label for="p75">First</label>
<input id="p30" type="radio" name="geodesic_size" value="5626" checked>
<label for="p30" class="active">Second</label>

about 4 years ago · Juan Pablo Isaza Denunciar

0

The other answers seem to work fine, I would just add a couple suggestions:

First, you can also access the label(s) associated to an element with HTMLInputElement.labels. Using el.labels[0] would IMO be a bit more robust than relying on the index of the radio button and the label to be the same.

If all you need to do is toggle an active class like this, you could accomplish the same thing with CSS only by using the adjacent sibling combinator (+) and get rid of all the JavaScript:

input:checked + label {
  color: red;
}
<div>
  <input id="p75" type="radio" name="geodesic_size" value="11250">
  <label for="p75">p75</label>
</div>
<div>
  <input id="p30" type="radio" name="geodesic_size" value="5626" checked>
  <label for="p30">p30</label>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

  • There was no major change I have just add <div id="calculator"></div>.

As #calculator input[type=radio] this means select all input with type equals to radio, which are children of id #calculator.

  • Then selected all inputs loop through them attach onclick event and put you logic for active() function there.
  • To see visual effect added .active{background-color:red;} css.

var radios = document.querySelectorAll('#calculator input[type=radio]');
var label = document.querySelectorAll("#calculator label");
radios.forEach(radioBtn => {
  radioBtn.onclick = function() {
    radios.forEach((el, index) => {
      console.log(el.checked);
      if (el.checked === true) {
        console.log('true');
        label[index].classList.add('active');
      } else {
        console.log('false');
        label[index].classList.remove('active');
      }
    })
  };
})
.active {
  background-color: red;
}
<div id="calculator">
  <input id="p75" type="radio" name="geodesic_size" value="11250">
  <label for="p75">p75</label>
  <input id="p30" type="radio" name="geodesic_size" value="5626" checked>
  <label for="p30">p30</label>
</div>

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