Drago
Drago

Reputation: 1885

Select all elements that don't start with a certain id

Is there a way to get all the elements that don't start with the id foo in JavaScript?

I tried the following:

var elements = document.querySelectorAll('[id!=foo]');

That doesn't work.

Basically I want the opposite of:

var elements = document.querySelectorAll('[id^=foo]');

Upvotes: 1

Views: 1290

Answers (3)

user47589
user47589

Reputation:

Use the :not() selector:

document.querySelectorAll(":not([id^='foo'])");

Upvotes: 4

JJWesterkamp
JJWesterkamp

Reputation: 7916

You can use the :not pseudo selector to match everything except [id^="foo"]:

var elements = document.querySelectorAll(':not([id^=foo])');

Upvotes: 2

ControlAltDel
ControlAltDel

Reputation: 35096

Just select all and then filter out the one with the id

document.querySelectorAll("*").filter(...)

Upvotes: 0

Related Questions