Click the button to add the "mystyle" class to DIV. I am a DIV element
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>
How to Use of the class attribute in an HTML document - | HTML Global Attributes |
How to add Add multiple classes to one HTML element | HTML Global Attributes |
How to use JavaScript to add a yellow background color to the first element with class="example" (index 0) | HTML Global Attributes |
How to use JavaScript to add the "mystyle" class to an element with id="myDIV" | HTML Global Attributes |
Type: | Html |
Category: | Web Tutorial |
Sub Category: | HTML Global Attributes |
Uploaded by: | Admin |