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

237
Vistas
How do I dynamically get the value of an element from an array of elements?

I have a form with 3 checkboxes. I'm trying to the value of whichever checkbox is clicked on. I'm able to get the value of a hardcoded checkbox index (checkbox[0] for example), but I can't get the value of checkbox[i] with vanilla JS.

document.addEventListener("DOMContentLoaded", function() {

  var checkboxes = document.getElementsByClassName('checkbox');
  var listType = document.getElementById('ListType');
  for (var i = 0; i < checkboxes.length; i++) {

    checkboxes[i].addEventListener('click', function() {

      var inputByIndex = checkboxes[0].value; //I can get the value of the first element, but I can't get the value of whichever checkbox is checked. checkbox[i] doesn't work.
      listType.classList.add(inputByIndex);
      var spanType = document.getElementById("type");
      spanType.innerText = inputByIndex;

    });
  }
});
input {
  margin: 20px;
}

#ListType.basiclist {
  color: red;
}

#ListType.accordionlist {
  color: blue;
}

#ListType.internalonly {
  color: pink;
}
<form id="ListTypes">
  <label for "basicList"><input type="checkbox" id="basicList" class="checkbox" name="basicList" value="basiclist"/>Basic List</label>
  <label for "accordionList"><input type="checkbox" id="accordionList" class="checkbox" name="accordionList" value="accordionlist"/>Accordion List</label>
  <label for "internalOnly"><input type="checkbox" id="internalOnly" class="checkbox" name="internalOnly" value="internalonly" />Internal Use Only</label>
</form>

<div id="ListType">
  List Type: <span id="type"></span>
</div>

Fiddle

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

0

You can use event.currentTarget to access the element on which event has occurred.

The currentTarget read-only property of the Event interface identifies the current target for the event, as the event traverses the DOM.

document.addEventListener("DOMContentLoaded", function() {
  var checkboxes = document.getElementsByClassName('checkbox');
  var listType = document.getElementById('ListType');
  for (var i = 0; i < checkboxes.length; i++) {

    checkboxes[i].addEventListener('click', function(event) {
      var inputByIndex = event.currentTarget.value;
      listType.classList.add(inputByIndex);
      var spanType = document.getElementById("type");
      spanType.innerText = inputByIndex;

    });
  }
});
input {
  margin: 20px;
}

#ListType.basiclist {
  color: red;
}

#ListType.accordionlist {
  color: blue;
}

#ListType.internalonly {
  color: pink;
}
<form id="ListTypes">
  <label for "basicList"><input type="checkbox" id="basicList" class="checkbox" name="basicList" value="basiclist"/>Basic List</label>
  <label for "accordionList"><input type="checkbox" id="accordionList" class="checkbox" name="accordionList" value="accordionlist"/>Accordion List</label>
  <label for "internalOnly"><input type="checkbox" id="internalOnly" class="checkbox" name="internalOnly" value="internalonly" />Internal Use Only</label>
</form>

<div id="ListType">
  List Type: <span id="type"></span>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Delegate

You need to think of the CSS for more than one listType color or use a set of radio buttons

document.addEventListener("DOMContentLoaded", function() {
  document.getElementById('ListTypes').addEventListener("click", function(e) {
    const tgt = e.target;
    if (tgt.type && tgt.type === 'checkbox') { 
      const values = [...tgt.form.querySelectorAll("[type=checkbox]:checked")].map(chk => chk.value);
      document.getElementById("type").textContent = values.join(", ")
      document.getElementById("ListType").classList.add(...values);
    }
  });
});
input {
  margin: 20px;
}

#ListType.basiclist {
  color: red;
}

#ListType.accordionlist {
  color: blue;
}

#ListType.internalonly {
  color: pink;
}
<form id="ListTypes">
  <label for "basicList"><input type="checkbox" id="basicList" class="checkbox" name="basicList" value="basiclist"/>Basic List</label>
  <label for "accordionList"><input type="checkbox" id="accordionList" class="checkbox" name="accordionList" value="accordionlist"/>Accordion List</label>
  <label for "internalOnly"><input type="checkbox" id="internalOnly" class="checkbox" name="internalOnly" value="internalonly" />Internal Use Only</label>
</form>

<div id="ListType">
  List Type: <span id="type"></span>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

the checkboxes list doesn't exist within the closure of the onclick funcion. Instead use this.value.

JS fiddle

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