HowTo Home


Icon Bar Menu Icon Accordion Tabs Vertical Tabs Tab Headers Top Navigation Responsive Topnav Search Bar Side Navigation Fullscreen Navigation Off-Canvas Menu Hover Sidenav Buttons Horizontal Scroll Menu Vertical Menu Bottom Navigation Responsive Bottom Nav Bottom Border Nav Links Fixed Menu Slide Down Bar on Scroll Sticky Navbar Hover Dropdowns Click Dropdowns Dropdown in Navbar Mega Menu Pagination Breadcrumbs Button Group Vertical Button Group


Slideshow Modal Images Lightbox Responsive Image Grid Image Grid Tab Gallery Image Overlay Image Effects Image Text Transparent Image Text Full Page Image Hero Image Rounded Images Meet the Team Sticky Image Flip an Image Shake an Image Portfolio Gallery Portfolio with Filtering


Alert Buttons Animated Buttons Social Media Buttons Loading Buttons Next/prev Buttons Round Buttons Scroll To Top Button


Login Form Signup Form Contact Form Newsletter Responsive Form Clear Input Field Copy Text to Clipboard Animated Search Custom Checkbox/Radio Toggle Switch Password Validation Toggle Password Visibility Multiple Step Form


Filter List Filter Table Filter Elements Sort List Sort Table


Fullscreen Video Modal Boxes Timeline Progress Bars Skill Bar Range Sliders Tooltips Popups Calendar Zebra Striped Table Responsive Tables HTML Includes To Do List Loaders Star Rating User Rating Overlay Effect Contact Chips Cards Profile Card Alerts Notes Labels Circles Coupon Fixed Footer Equal Height Clearfix Snackbar Scroll Drawing Pricing Table Parallax Aspect Ratio Build a Web Site Toggle Like/Dislike Toggle Hide/Show Toggle Class Add Class Remove Class Arrows Shapes Browser Window Custom Scrollbar Device Look Placeholder Color Vertical Line Animate Icons Countdown Timer Typewriter Coming Soon Page Center Website Thumbnails Chat Messages Testimonials Draggable HTML Element Syntax Highlighter JS Animations Contact Section


2 Column Layout 3 Column Layout 4 Column Layout Expanding Grid Mixed Column Layout


Google Maps Google Translate Google Charts Google Fonts


Convert Weight Convert Temperature Convert Length Convert Speed

How TO - Add a Class

Learn how to add a class name to an element with JavaScript.

Click the button to add a class to me!

Add Class

Step 1) Add HTML:

Add a class name to the div element with id="myDIV" (in this example we use a button to add the class).


<button onclick="myFunction()">Try it</button>

<div id="myDIV">
  This is a DIV element.

Step 2) Add CSS:

Style the specified class name:


.mystyle {
    width: 100%;
    padding: 25px;
    background-color: coral;
    color: white;
    font-size: 25px;

Step 3) Add JavaScript:

Get the <div> element with id="myDIV" and add the "mystyle" class to it:


function myFunction() {
    var element = document.getElementById("myDIV");
Try it Yourself »

Cross-browser solution

Note: The classList property is not supported in Internet Explorer 9. The following code will work in all browsers:


function myFunction() {
    var element, name, arr;
    element = document.getElementById("myDIV");
    name = "mystyle";
    arr = element.className.split(" ");
    if (arr.indexOf(name) == -1) {
        element.className += " " + name;
Try it Yourself »

Tip: Also see How To Toggle A Class.

Tip: Also see How To Remove A Class.

Tip: Learn more about the classList property in our JavaScript Reference.

Tip: Learn more about the className property in our JavaScript Reference.