querySelectorAll checked values
Asked Answered
E

1

14

I have a list/group of checkboxes, that made by php; put in name="item[]" html property, and then follow with this:

 var list = document.querySelectorAll("input[name^='item[']")

So far, works well. I need to count only the checked ones, not all like:

 list.length

How can I access the values for eachone to check "checked" property or value?

Esposito answered 7/6, 2017 at 16:8 Comment(0)
M
32

Rather than looking through your list, you can go back and ask querySelectorAll to return just the checked items:

var checkedList = document.querySelectorAll("input[name^='item[']:checked")
Micronesia answered 7/6, 2017 at 16:11 Comment(2)
typo: extra [ in "input[name^='item[']:checked"Expend
@Expend There isn’t - the one in the single quotes (item[) is part of the name we’re matching against, the brackets outside of that surround the entire attribute of the input element we’re looking for.Micronesia

© 2022 - 2024 — McMap. All rights reserved.