Pumych
Pumych

Reputation: 1398

Pure JS, get element by class but not class

var items = document.getElementsByClassName("classname");

Gives me all the .classname classes, how do I update the code to get all those classes but excluding .classname_exclude?

Upvotes: 2

Views: 2081

Answers (1)

Josh Crozier
Josh Crozier

Reputation: 240928

Rather than using the method .getElementsByClassName(), you could use the method .querySelectorAll() (which accepts CSS3 selectors) and use the :not() pseudo class to negate those elements:

var items = document.querySelectorAll('.classname:not(.classname_exclude)');

Upvotes: 10

Related Questions