最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get the value of checked checkbox in vanilla JavaScript? - Stack Overflow

programmeradmin3浏览0评论

I am working on this snippet. Why am I not able to get the value of any checked checkbox with class .shapes?

document.getElementsByName('shapes').onclick = function() {
  var checkboxes = document.getElementsByName('shapes')[0].value;
  console.log(checkbox.value);
}
<div class="js-shapes">
  <span class="ib">
          <input type="checkbox" name="shapes" class="shapes" value="circle" id="cb-circle"> <label for="cb-circle">Circle</label>
        </span>
  <span class="ib">
          <input type="checkbox" name="shapes" class="shapes" value="diamond" id="cb-diamond"> <label for="cb-diamond">Diamond</label>
        </span>
  <span class="ib">
          <input type="checkbox" name="shapes" class="shapes" value="square" id="cb-square"> <label for="cb-square">Square</label>
        </span>
  <span class="ib">
          <input type="checkbox" name="shapes" class="shapes" value="triangle" id="cb-triangle"> <label for="cb-triangle">Triangle</label>
        </span>
  <span class="ib">
          <input type="checkbox" name="shapes" class="shapes" value="all" id="cb-all" checked> <label for="cb-all">all Shapes</label>
        </span>

</div>

I am working on this snippet. Why am I not able to get the value of any checked checkbox with class .shapes?

document.getElementsByName('shapes').onclick = function() {
  var checkboxes = document.getElementsByName('shapes')[0].value;
  console.log(checkbox.value);
}
<div class="js-shapes">
  <span class="ib">
          <input type="checkbox" name="shapes" class="shapes" value="circle" id="cb-circle"> <label for="cb-circle">Circle</label>
        </span>
  <span class="ib">
          <input type="checkbox" name="shapes" class="shapes" value="diamond" id="cb-diamond"> <label for="cb-diamond">Diamond</label>
        </span>
  <span class="ib">
          <input type="checkbox" name="shapes" class="shapes" value="square" id="cb-square"> <label for="cb-square">Square</label>
        </span>
  <span class="ib">
          <input type="checkbox" name="shapes" class="shapes" value="triangle" id="cb-triangle"> <label for="cb-triangle">Triangle</label>
        </span>
  <span class="ib">
          <input type="checkbox" name="shapes" class="shapes" value="all" id="cb-all" checked> <label for="cb-all">all Shapes</label>
        </span>

</div>

Share Improve this question edited Aug 9, 2020 at 22:58 halfer 20.4k19 gold badges108 silver badges201 bronze badges asked Aug 9, 2020 at 4:34 BehseiniBehseini 6,32823 gold badges86 silver badges137 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

You can simply use querySelectorAll method and use forEach method to check which checkbox was clicked using addEventListener with change function in it.

Edit: If you want to get the value of checkbox when checked you can checked property and then display its value.

Live Demo

let allCheckBox = document.querySelectorAll('.shapes')

  allCheckBox.forEach((checkbox) => { 
  checkbox.addEventListener('change', (event) => {
    if (event.target.checked) {
      console.log(event.target.value)
    }
  })
})
<div class="js-shapes">
  <span class="ib">
    <input type="checkbox" name="shapes" class="shapes" value="circle" id="cb-circle"> <label for="cb-circle">Circle</label>
  </span>
  <span class="ib">
    <input type="checkbox" name="shapes" class="shapes" value="diamond" id="cb-diamond"> <label for="cb-diamond">Diamond</label>
  </span>
  <span class="ib">
    <input type="checkbox" name="shapes" class="shapes" value="square" id="cb-square"> <label for="cb-square">Square</label>
  </span>
  <span class="ib">
    <input type="checkbox" name="shapes" class="shapes" value="triangle" id="cb-triangle"> <label for="cb-triangle">Triangle</label>
  </span>
  <span class="ib">
    <input type="checkbox" name="shapes" class="shapes" value="all" id="cb-all" checked> <label for="cb-all">all Shapes</label>
  </span>

</div>

getElementsByName() returns collection, you have to loop through them so that you can attach the event to them individually. Also, I will suggest you to use addEventListener() to attach event:

document.getElementsByName('shapes').forEach(function(chk){
  chk.addEventListener('click', function() {
    if(this.checked){
      console.log(this.value);
    }
  });
});
<div class="js-shapes">
  <span class="ib">
    <input type="checkbox" name="shapes" class="shapes" value="circle" id="cb-circle"> <label for="cb-circle">Circle</label>
  </span>
  <span class="ib">
    <input type="checkbox" name="shapes" class="shapes" value="diamond" id="cb-diamond"> <label for="cb-diamond">Diamond</label>
  </span>
  <span class="ib">
    <input type="checkbox" name="shapes" class="shapes" value="square" id="cb-square"> <label for="cb-square">Square</label>
  </span>
  <span class="ib">
    <input type="checkbox" name="shapes" class="shapes" value="triangle" id="cb-triangle"> <label for="cb-triangle">Triangle</label>
  </span>
  <span class="ib">
    <input type="checkbox" name="shapes" class="shapes" value="all" id="cb-all" checked> <label for="cb-all">all Shapes</label>
  </span>
</div>

发布评论

评论列表(0)

  1. 暂无评论