Sometimes, we want to get multiple elements with multiple classes with JavaScript.
In this article, we’ll look at how to get HTML elements with multiple classes with JavaScript.
Get HTML Elements with Both Classes
To get HTML elements with both classes, we can use the getElementsByClassName
or querySelectorAll
methods.
For instance, if we have the following HTML:
<div class='class1'>
foo
</div>
<div class='class2'>
bar
</div>
<div class='class1 class2'>
baz
</div>
Then we can get the div with text ‘baz’ by writing:
const list1 = document.getElementsByClassName("class1 class2");
const list2 = document.querySelectorAll(".class1.class2");
console.log(list1)
console.log(list2)
We call getElementsByClassName
with 'class1 class2'
to get the div with both class1
and class2
present.
Likewise, we can do the same with querySelectorAll
by using the “.class1.class2”
CSS selector.
Then list1
is the HTMLCollection with the 3rd div.
And list2
is the NodeList with the 3rd div.
Get HTML Elements with At Least One Class
We can get HTML elements with at least one class by using the querySelector
method.
For instance, if we have the following HTML:
<div class='class1'>
foo
</div>
<div class='class2'>
bar
</div>
<div class='class1 class2'>
baz
</div>
Then we can write:
const list = document.querySelectorAll(".class1,.class2");
console.log(list)
We use “.class1,.class2”
with querySelectorAll
to get elements with class1
or class2
or both as the class.
And so list
is a NodeList with all 3 elements.
Get HTML Elements with One Class But Not Both
We can also use querySelector
to get HTML elements with one class but not both.
For instance, if we have the following HTML:
<div class='class1'>
foo
</div>
<div class='class2'>
bar
</div>
<div class='class1 class2'>
baz
</div>
Then we write:
const list = document.querySelectorAll(".class1:not(.class2),.class2:not(.class1)");
console.log(list)
We use the :not
pseudo-selector to exclude class2
with class1
and class1
with class2
.
So list
is a NodeList with the first 2 divs.
Get HTML Elements with None of the Classes or One Class Only
To get all the elements with none of the classes or only one of the classes applied to it, we can use the :not
pseudo-selector again.
For instance, if we have the following HTML:
<div class='class1'>
foo
</div>
<div class='class2'>
bar
</div>
<div class='class1 class2'>
baz
</div>
Then we can write:
const list = document.querySelectorAll(":not(.class1),:not(.class2)");
console.log(list)
And we get all the elements in the page but the div with text baz.
Get HTML Elements with None of the Classes
To get the HTML elements with none of the classes, we can use the :not
pseudo-selector again.
For instance, if we have the following HTML:
<div class='class1'>
foo
</div>
<div class='class2'>
bar
</div>
<div class='class1 class2'>
baz
</div>
Then we can write:
const list = document.querySelectorAll(":not(.class1):not(.class2)");
console.log(list)
to select anything but the divs with class1
or class2
.
Conclusion
We can use querySelector
to select element with any combinations of the classes applied to an element we want.