0

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 ?

MShack
  • 642
  • 1
  • 14
  • 33
  • You are missing what `querySelectorAll()` returns. Look it up in the docs and you'll see, and will understand how to make it work. – Asons May 05 '19 at 17:12

2 Answers2

4

querySelectorAll returns a NodeList. You will need to check the length like this: if (!document.querySelectorAll('#body_options_45, #body_options_113').length)

Mati Tucci
  • 2,826
  • 5
  • 28
  • 40
0

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
}
ludovico
  • 2,103
  • 1
  • 13
  • 32