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

168
Vistas
Put value null if none of checkbox is selected
    get_genre_value=this.value; //setting value of current check box to get_genre_value.

Here i had globally declared var get_genre_value; which is necessary for my project and I am taking the value from get_genre_value on button click. Selecting checkbox value and getting it was easy but how am I able to set the value to null for get_genre_value if no check box is selected this got me thinking hard, as the get_genre_value will use its value stored globbaly if I try to click the button second time without selecting any checkbox , any help would be much appreciated.

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Here is an example how to do it: You set get_genre_value to null in the beginning. For each checkbox you add an EventListener. In the callback you check, if the checkbox is checked:

  • It is: You uncheck the checkbox that was checked before (it is perhaps null if no checkbox was checked before) and set get_genre_value to the value of the checkbox
  • It is not: You set get_genre_value to null again

let get_genre_value = null;

const checkboxes = document.querySelectorAll("input[name=select]");

checkboxes.forEach((checkbox) => {
  checkbox.addEventListener("change", (event) => {
    if (event.target.checked) {
      const checkedbefore = document.querySelector(`input[name=select][value=\"${get_genre_value}\"]`);
      
      if (checkedbefore !== null)
        checkedbefore.checked = false;

      get_genre_value = event.target.value;
    } else {
      get_genre_value = null;
    }
    
    console.log(get_genre_value);
  });
});
<input type="checkbox" name="select" value="1">
<input type="checkbox" name="select" value="2">
<input type="checkbox" name="select" value="3">
<input type="checkbox" name="select" value="4">

about 4 years ago · Santiago Trujillo 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