Horje

How to use JavaScript to add the "mystyle" class to an element with id="myDIV"

Click the button to add the "mystyle" class to DIV. I am a DIV element

index.html
Example: HTML
<!DOCTYPE html>
<html>
<head>
<style>
.mystyle {
  margin: 20px;
  width: 300px;
  height: 50px;
  background-color: coral;
  color: white;
  font-size: 25px;
}
</style>
</head>
<body>

<p>Click the button to add the "mystyle" class to DIV.</p>

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

<div id="myDIV">
I am a DIV element
</div>
<br>

<script>
function myFunction() {
  document.getElementById("myDIV").classList.add("mystyle");
}
</script>

</body>
</html>

Output should be:

How to use JavaScript to add the "mystyle" class to an element with id="myDIV"




Type:
html
Category:
Web Tutorial
Sub Category:
HTML Global Attributes
Uploaded by:
Admin