• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using Javascript to Remove Class from Element

Using Javascript to Remove Class from Element

December 1, 2021 Leave a Comment

To remove a class from an HTML element using Javascript, the simplest way is to get the classList of the element and then use the remove() method.

document.getElementById("div1").classList.remove("old-class");

Let’s say I have the following HTML:

<div id="div1" class="existing-class">
  <p class="p">This is a paragraph.</p>
</div>

If we want to remove the class “existing-class” from #div1, we can use Javascript to get the element, get the element’s classList, and then remove the class from its classList.

function clickFunction() {
  var divToRemoveClass = document.getElementById("div1");
  divToRemoveClass.classList.remove("existing-class");
}

The resulting HTML would be as follows:

<div id="div1" class="">
  <p class="p">This is a paragraph.</p>
</div>

Removing a Class of an HTML Element Using Javascript With a Click

We can remove a class from an HTML element using Javascript very easily by combining the remove() method with a click event.

Let’s say we have the following HTML code and we want to give the user the ability to remove an existing class from the paragraph. The new class will remove the underline from the paragraph content.

<style>.underline-class { text-decoration:underline; }</style>
<div id="div1">
  <p id="p1" class="underline-class">This is the paragraph we will remove a class from.</p>
  <div class="click-me" onclick="clickFunction()">Remove class</div>
</div>

We can utilize both the onclick event and the remove() method to remove a class from the paragraph and remove the underline from the paragraph.

Below is the Javascript code which will allow the user to be able to remove the class from the paragraph:

function clickFunction() {
     var currP = document.getElementById("p1");
     currP.classList.remove("underline-class");
}

The final code and output for this example of how to remove a class using Javascript is below:

Code Output:

This is the paragraph we will remove a class from.

Remove class

Full Code:

<style>.underline-class { text-decoration:underline; }</style>
<div id="div1">
  <p id="p1" class="underline-class">This is the paragraph we will remove a class from.</p>
  <div class="click-me" onclick="clickFunction()">Remove class</div>
</div>

<script>

function clickFunction() {
     var currP = document.getElementById("p1");
     currP.classList.underline-class("underline-class");
}

</script>

Hopefully this article has been useful for you to understand how to remove a class from an HTML element using Javascript.

Other Articles You'll Also Like:

  • 1.  Convert String to Float in JavaScript
  • 2.  How to Filter a List of Divs with a Text Input Bar Using Javascript
  • 3.  Using JavaScript to Square a Number
  • 4.  Remove String From Array in JavaScript
  • 5.  Remove Commas From a String in JavaScript
  • 6.  Using JavaScript to Redirect to a Relative URL
  • 7.  JavaScript Opacity – How to Change the Opacity of an Element Using JavaScript
  • 8.  clearInterval JavaScript – How the clearInterval() Method in JavaScript Works
  • 9.  Finding the Length of a String in JavaScript
  • 10.  Using JavaScript to Get Date Format in yyyy-mm-dd hh mm ss

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy