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
Release: 2024-12-25 09:52:36
Original
673 people have browsed it

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");
Copy after login

Example:

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

HTML:

<div>
Copy after login

CSS:

.red {
  background: red
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template