Home >Web Front-end >CSS Tutorial >How Can I Remove a CSS Class with JavaScript Without Using jQuery?

How Can I Remove a CSS Class with JavaScript Without Using jQuery?

Susan Sarandon
Susan SarandonOriginal
2024-12-25 09:52:36631browse

How Can I Remove a CSS Class with JavaScript Without Using jQuery?

Removing CSS Class with JavaScript without jQuery

To eliminate a class on an element in JavaScript, steer clear of jQuery and try out the classList property. Widely supported in modern browsers, it offers a standardized solution:

Code:

ELEMENT.classList.remove("CLASS_NAME");

Example:

const remove = () => {
  const el = document.querySelector('#el');
  el.classList.remove("red");
};

HTML:

<div>

CSS:

.red {
  background: red
}

By implementing classList.remove(), you can effortlessly remove a specified CSS class from an element, enabling dynamic control over the element's appearance without relying on jQuery.

The above is the detailed content of How Can I Remove a CSS Class with JavaScript Without Using jQuery?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn