Home > Web Front-end > JS Tutorial > How do you remove a CSS class from an element using pure JavaScript?

How do you remove a CSS class from an element using pure JavaScript?

Mary-Kate Olsen
Release: 2024-11-08 17:06:02
Original
348 people have browsed it

How do you remove a CSS class from an element using pure JavaScript?

Removing CSS Class from an Element Using Pure JavaScript

When working with web elements, you may encounter situations where you need to dynamically remove a specific CSS class. Knowing how to achieve this using pure JavaScript is crucial. Unlike jQuery, which simplifies element manipulation, JavaScript offers a versatile solution for class removal.

To effectively remove a class from an element using JavaScript, leverage the classList property, which is widely supported by modern browsers. The syntax is straightforward:

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

For instance, let's consider the following HTML element:

<div>
Copy after login

In this case, el is an element with an assigned red class. Suppose you want to remove the red class from it using JavaScript. You can do so by executing the following code:

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

After this execution, the el element will no longer have the red class, effectively changing its appearance. Note that this solution is compatible with most modern browsers and provides a clean way to manipulate CSS classes dynamically.

The above is the detailed content of How do you remove a CSS class from an 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