How to Get Class List of an Element with jQuery?
jQuery is not just a JavaScript library, it is a powerful tool that can be used to develop very effective web applications. The above jQuery tutorial clearly explains how to get a class list of an element with jQuery, which is useful for many projects in the future. This example uses the document ready event, where we call the parent function on document.ready(), and then we call the child function when the DOM is ready.
js check for class in classList
on Jan 01, 1970
element.classList.contains(className);
0
classList has class
on Jan 01, 1970
if (document.body.classList.contains('thatClass')) {
// do some stuff
}
0
js classlist
on Jan 01, 1970
classList.item(index); // Returns the item in the list by its index, or undefined if index is greater than or equal to the list's length
classList.contains(token); // Returns true if the list contains the given token, otherwise false.
classList.add(token1[, ...tokenN]); // Adds the specified token(s) to the list.
classList.remove(token1[, ...tokenN]); // Removes the specified token(s) from the list.
classList.replace(oldToken, newToken); // Replaces token with newToken.
classList.supports(token); // Returns true if a given token is in the associated attribute's supported tokens.
classList.toggle(token[, force]); // Removes token from the list if it exists, or adds token to the list if it doesn't. Returns a boolean indicating whether token is in the list after the operation.
classList.entries(); // Returns an iterator, allowing you to go through all key/value pairs contained in this object.
classList.forEach(callback[ ,thisArg]); // Executes a provided callback function once per DOMTokenList element.
classList.keys(); // Returns an iterator, allowing you to go through all keys of the key/value pairs contained in this object.
classList.values(); // Returns an iterator, allowing you to go through all values of the key/value pairs contained in this object.
0
ClassList
on Jan 01, 1970
const div = document.createElement('div');
div.className = 'foo';
// our starting state: <div class="foo"></div>
console.log(div.outerHTML);
// use the classList API to remove and add classes
div.classList.remove("foo");
div.classList.add("anotherclass");
// <div class="anotherclass"></div>
console.log(div.outerHTML);
// if visible is set remove it, otherwise add it
div.classList.toggle("visible");
// add/remove visible, depending on test conditional, i less than 10
div.classList.toggle("visible", i < 10 );
console.log(div.classList.contains("foo"));
// add or remove multiple classes
div.classList.add("foo", "bar", "baz");
div.classList.remove("foo", "bar", "baz");
// add or remove multiple classes using spread syntax
const cls = ["foo", "bar"];
div.classList.add(...cls);
div.classList.remove(...cls);
// replace class "foo" with class "bar"
div.classList.replace("foo", "bar");
0
JS classList
on Jan 01, 1970
-- HTML --
<div class='container'></div>
-- JAVASCRIPT --
element.classList.contains('container'); // True or false
element.classList.add('my-class'); // Adds the class if it isn't present yet
element.classList.remove('my-class'); // Removes the class if it’s present
element.classList.toggle('my-class'); // Adds if not present, else removes
0
It's worth noting that this won't work on things like form elements and such, but it should work on any HTML element. This is great for simple CSS styling or general layout purposes.
Javascript answers related to "Classlist contains"
Classlist contains Classlist.toggle Classlist contains Classlist.toggle Classlist.toggle Classlist contains javascript classlist add Classlist contains Classlist contains Classlist.toggle javascript classlist add Classlist contains javascript classlist add Cannot read property 'classList' of undefined
View All Javascript queries
Javascript queries related to "Classlist contains"
Browse Other Code Languages
Abap ActionScript Assembly BASIC C C# C++ Clojure Cobol CSS Dart Delphi Elixir Erlang F# Fortran Go Groovy Haskell Html Java Javascript Julia Kotlin Lisp Lua Matlab Objective-C Pascal Perl PHP PostScript Prolog Python R Ruby Rust Scala Scheme Shell/Bash Smalltalk SQL Swift TypeScript VBA WebAssembly Whatever