×

Save Your Code

If you click the save button, your code will be saved, and you get an URL you can share with others.

By clicking the "Save" button you agree to our terms and conditions.

Report Error

×

Save to Google Drive

If you have a Google account, you can save this code to your Google Drive.

Google will ask you to confirm Google Drive access.

×

Open from Google Drive

If you have saved a file to Google Drive, you can open it here:

Result Size: 625 x 571
x
 
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.mystyle {
  width: 100%;
  padding: 25px;
  background-color: coral;
  color: white;
  font-size: 25px;
  box-sizing: border-box;
}
</style>
</head>
<body>
<p>Click the "Try it" button to toggle between adding and removing the "mystyle" class name of the DIV element:</p>
<p><strong>Note:</strong> The classList property is not supported in IE9 and earlier. In this example, we check if the browser supports the classList.toggle() method. If not, use the className property together with other JS properties and methods to achieve the same result (for IE9).</p>
<button onclick="myFunction()">Try it</button>
<div id="myDIV">
This is a DIV element.
</div>
<script>
function myFunction() {
  var element = document.getElementById("myDIV");
  if (element.classList) { 
    element.classList.toggle("mystyle");
  } else {
    var classes = element.className.split(" ");
    var i = classes.indexOf("mystyle");
    if (i >= 0) 
      classes.splice(i, 1);
    else 
      classes.push("mystyle");
      element.className = classes.join(" "); 
  }
}
×

Report a Problem: