Element className Property - Overwriting an existing class name with a new one: - Javascript DOM

Javascript examples for DOM:Element className

Description

Element className Property - Overwriting an existing class name with a new one:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
.mystyle {//w ww.  j ava2  s  . c  o  m
    width: 300px;
    height: 50px;
    background-color: coral;
    color: white;
    margin-bottom: 10px;
}

.newClassName {
    width: 400px;
    height: 100px;
    background-color: lightblue;
    text-align: center;
    font-size: 25px;
    color: navy;
    margin-bottom: 10px;
}
</style>
</head>
<body>

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

<button onclick="myFunction()">Test</button>

<script>
function myFunction() {
    document.getElementById("myDIV").className = "newClassName";
}
</script>

</body>
</html>

Related Tutorials