I have found this accepted answer but somehow it doesn't work for me:
var checkedValue = null;
var inputElements = document.getElementsByClassName('messageCheckbox');
for(var i=0; inputElements[i]; ++i){
if(inputElements[i].checked){
checkedValue = inputElements[i].value;
break;
}
}
when I tried to log inputElements[0] it logs:
<input type="checkbox" id="checkbox-2" class="todo-checkbox" onclick="handleCheck('text-2', 'checkbox-2')">
I only want to use pure js, no jquery, help?
To get the count of all checked checkbox you can use filter() like the following way:
var inputElements = [].slice.call(document.querySelectorAll('.messageCheckbox'));
var checkedValue = inputElements.filter(chk => chk.checked).length;
console.log(checkedValue);
<input type="checkbox" class="messageCheckbox" checked>
<input type="checkbox" class="messageCheckbox">
<input type="checkbox" class="messageCheckbox" checked>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With