membersound
membersound

Reputation: 86847

How to exclude specific class names in querySelectorAll()?

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?

Upvotes: 80

Views: 65558

Answers (3)

Nazakat Ali
Nazakat Ali

Reputation: 80

Element by tagName and ignore or exclude element that have a specific class:

document.querySelectorAll(`svg,div:not([class*="background"]`)

Upvotes: 5

James Donnelly
James Donnelly

Reputation: 128776

Use the CSS's negation pseudo-selector, :not():

document.querySelectorAll('span.test:not(.asd)');

The negation pseudo-class, :not(X), is a functional notation taking a simple selector (excluding the negation pseudo-class itself) as an argument. It represents an element that is not represented by its argument.

Upvotes: 28

VisioN
VisioN

Reputation: 145428

Use :not CSS pseudo-class:

document.querySelectorAll('span.test:not(.asd)');

Upvotes: 129

Related Questions