chadb768
chadb768

Reputation: 473

Get all elements except ones from a certain class?

I currently have a JavaScript function that uses getElementsByClassName("apples") to find everything in the class apples. My problem is that I can't seem to find a way to fetch every element not in the class apples. Is there any helper functions in JavaScript that might allow me to do this? Thanks for the help!

Upvotes: 1

Views: 2977

Answers (1)

Alnitak
Alnitak

Reputation: 339816

You can't do it with getElementsByClassName.

The easiest way on modern browsers would be to use:

document.querySelectorAll(':not(.apples)')

On older browsers you'd probably have to use document.getElementsByTagName('*') followed by a filtering operation to remove the unwanted elements. This would probably be very slow.

Upvotes: 8

Related Questions