patternjavascriptCritical
Remove CSS class from element with JavaScript (no jQuery)
Viewed 0 times
withfromjqueryclassremovecsselementjavascript
Problem
Could anyone let me know how to remove a class on an element using JavaScript only?
Please do not give me an answer with jQuery as I can't use it, and I don't know anything about it.
Please do not give me an answer with jQuery as I can't use it, and I don't know anything about it.
Solution
The right and standard way to do it is using
Documentation: https://developer.mozilla.org/en/DOM/element.classList
classList. It is now widely supported in the latest version of most modern browsers:ELEMENT.classList.remove("CLASS_NAME");remove.onclick = () => {
const el = document.querySelector('#el');
el.classList.remove("red");
}
.red {
background: red
}
Test
Remove Class
Documentation: https://developer.mozilla.org/en/DOM/element.classList
Code Snippets
ELEMENT.classList.remove("CLASS_NAME");Context
Stack Overflow Q#2155737, score: 1173
Revisions (0)
No revisions yet.