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
2 Answers
Reset to default 7You 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>