Element id Property - If the first <div> element in the document has an id of "myDIV", change its font-size: - Javascript DOM

Javascript examples for DOM:Element id

Description

Element id Property - If the first <div> element in the document has an id of "myDIV", change its font-size:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
.mystyle {/*from  w  w  w  .  j  av a 2 s. co  m*/
    width: 300px;
    height: 50px;
    text-align: center;
    background-color: coral;
    color: white;
    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() {
    var x = document.getElementsByTagName("DIV")[0];

    if (x.id === "myDIV") {
        x.style.fontSize = "30px";
    }
}
</script>

</body>
</html>

Related Tutorials