Bijan
Bijan

Reputation: 8606

Javascript - Get Multiple Classes

How do I get either of multiple classes?

document.getElementsByClassName("F-rank-good"); gets all the elements that have the classname F-rank-good. How do I get all the elements that have classname F-rank-good OR F-rank-bad?

document.getElementsByClassName("F-rank-good F-rank-bad") gets all the elements that have BOTH of those classes but I want it so it has either class.

Upvotes: 0

Views: 134

Answers (1)

Sterling Archer
Sterling Archer

Reputation: 22405

getElementsByClassName does not support "or" as far as I know, but querySelectorAll does!

document.querySelectorAll(".F-rank-good, .F-rank-bad");

An easy way to see if getElementsByClassName does support it is to use the comma delimiter.

I suggest querySelectorAll either way, as the latter is supported in IE8

Upvotes: 5

Related Questions