Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

230
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!