Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
110
rated 0 times [  116] [ 6]  / answers: 1 / hits: 15522  / 8 Years ago, mon, april 11, 2016, 12:00:00

Let's say I want to find all div elements and span inside p.


Is it possible to get all what I want in a single querySelectorAll invocation?


Conceptually it should be something like document.querySelectorAll("div | p span") (where | means or).


More From » css

 Answers
59

Yes. You can use the same logical operators allowed in CSS:



OR: chain selectors with commas



document.querySelectorAll('div, p span');
// selects divs, and spans in ps


AND: chain selectors without whitespace



document.querySelectorAll('div.myClass');
// selects divs with the class myClass


NOT: :not()-selector



document.querySelectorAll('div:not(.myClass)');
// selects divs that do not have the class myClass

[#62621] Friday, April 8, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
carterf

Total Points: 255
Total Questions: 93
Total Answers: 122

Location: Marshall Islands
Member since Wed, Jun 17, 2020
4 Years ago
;