$.fn.hasClass()
Determine whether any node in the collection has the requested class. Returns as soon as a match is found.
Signatures
hasClass(cls: string): boolean;
Parameters
cls(string) — the class name to test for.
Returns
true when any node in the collection has the class, otherwise false.
Examples
import $ from "dabbyjs";
import "dabbyjs/attributes/hasclass/hasclass";
// Test whether any element matches
const isDark = $("body").hasClass("dark-mode");
// Test a specific element
const $card = $(".card").eq(1);
if ($card.hasClass("card--featured")) {
$card.addClass("card--highlighted");
}
// Conditional behaviour
$(".menu-button").on("click", () => {
const $nav = $(".navigation");
if ($nav.hasClass("navigation--open")) {
$nav.removeClass("navigation--open");
} else {
$nav.addClass("navigation--open");
}
});
See also
- $.fn.addClass(), $.fn.removeClass(), $.fn.toggleClass() — modify the class list.
- $.fn.attr() — read the
classattribute as a string.