Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude specific class names in querySelectorAll()?

Tags:

javascript

How can I exclude tag elements that have a specific class name?

<span class="test" /> <span class="test asd" />  document.querySelectorAll('span.test'); //how to exclude all spans with "asd" as class name? 
like image 527
membersound Avatar asked Sep 01 '14 11:09

membersound


People also ask

How do you exclude a class in CSS?

In CSS, to exclude a particular class, we can use the pseudo-class :not selector also known as negation pseudo-class or not selector. This selector is used to set the style to every element that is not the specified by given selector. Since it is used to prevent a specific items from list of selected items.

What does the querySelectorAll () method do?

The querySelectorAll() method in HTML is used to return a collection of an element's child elements that match a specified CSS selector(s), as a static NodeList object. The NodeList object represents a collection of nodes. The nodes can be accessed by index numbers.

How does the querySelector () method differ from querySelectorAll ()?

Differences: As seen above, querySelector() methodcan only be used to access a single element while querySelectorAll() method can be used to access all elements which match with a specified CSS selector. To return all matches, querySelectorAll has to be used, while to return a single match, querySelector is used.


1 Answers

Use :not CSS pseudo-class:

document.querySelectorAll('span.test:not(.asd)'); 
like image 142
VisioN Avatar answered Sep 18 '22 22:09

VisioN