• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / JavaScript / Using Javascript to Add Class to Element

Using Javascript to Add Class to Element

December 1, 2021 Leave a Comment

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

document.getElementById("div1").classList.add("new-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 add the class “new-class” to #div1, we can use Javascript to get the element, get the element’s classList, and then add the new class to its classList.

function clickFunction() {
  var divToAddClass = document.getElementById("div1");
  divToAddClass.classList.add("new-class");
}

The resulting HTML would be as follows:

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

Adding a Class to an HTML Element Using Javascript With a Click

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

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

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

We can utilize both the onclick event and the add() method to add a class to the paragraph and make the contents underlined.

Below is the Javascript code which will allow the user to be able to add a class to the paragraph:

function clickFunction() {
     var currP = document.getElementById("p1");
     currP.classList.add("p-new");
}

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

Code Output:

This is the paragraph we will add a class to.

Add class

Full Code:

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

<script>

function clickFunction() {
     var currP = document.getElementById("p1");
     currP.classList.add("p-new");
}

</script>

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

Other Articles You'll Also Like:

  • 1.  How to Change an Image on Hover Using JavaScript
  • 2.  Using JavaScript to Check a Radio Button
  • 3.  Grouping By Multiple Properties and Summing Using Javascript
  • 4.  getDate JavaScript – Get the Current Day of the Month in JavaScript
  • 5.  How to Clear a Textarea Field In JavaScript
  • 6.  JavaScript Sorted Map – How to Sort a Map by Keys or Values
  • 7.  Using JavaScript to Remove All Pipe Characters From String
  • 8.  How to Remove Non Numbers From String in JavaScript
  • 9.  Using JavaScript to Set Select to the First Option
  • 10.  Remove Undefined Values From an Array in JavaScript

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 *

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