Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Add Class to an Element with classList Property</title> <style> .info{ padding: 10px; border: 2px solid #ccc; } .highlight{ background: yellow; } </style> </head> <body> <div class="alert info" id="myDiv">The important piece of information!</div> <script> // Selecting element var elem = document.getElementById("myDiv"); elem.classList.add("highlight"); // Add a highlight class elem.classList.remove("alert"); // Remove alert class </script> </body> </html>