Home > Web Front-end > CSS Tutorial > How Can I Remove a Class from an HTML Element Using Pure JavaScript?

How Can I Remove a Class from an HTML Element Using Pure JavaScript?

Patricia Arquette
Release: 2024-12-23 02:36:18
Original
912 people have browsed it

How Can I Remove a Class from an HTML Element Using Pure JavaScript?

Element's Class Removal with Pure JavaScript

Removing classes from an element is a common task in web development, where JavaScript plays a crucial role. Here's how it's done without the use of jQuery:

The preferred and standardized method for class removal is through the classList property:

ELEMENT.classList.remove("CLASS_NAME");
Copy after login

This approach is widely supported by modern browsers. For instance:

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

Where:

  • remove is the button that triggers the removal.
  • el is the element whose class needs to be removed.
  • red is the class name to be removed.

In this example, clicking the button will remove the "red" class from the element with the ID "el," changing its background color back to its default.

The above is the detailed content of How Can I Remove a Class from an HTML Element Using Pure JavaScript?. 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