I currently have this working , but i need to add an additional element so i switched to SelectorAll , but it doesn't work
This is working
if(!document.getElementById('body_options_45')) {
//Do Stuff
}
When i add a 2nd ID and switch to selectorALl , it stops working
if(!document.querySelectorAll('#body_options_45, #body_options_113')) {
//Do Stuff
}
What am i missing here ?
querySelectorAll
returns a NodeList
. You will need to check the length
like this: if (!document.querySelectorAll('#body_options_45, #body_options_113').length)
querySelectorAll() returns a NodeList . NodeLists are not arrays but, in this particular case, the behavour is the same: empty arrays and empty NodeLists are both truthy .
![] //returns false
!emptyNodeList //returns false
To check if your NodeList is empty or not, use the length
property:
if(document.querySelectorAll('#body_options_45, #body_options_113').length===0) {
//do stuff
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.