Element id Property - Javascript DOM

Javascript examples for DOM:Element id

Description

The id property sets or gets the id of an element from id attribute.

Set the id property with the following Values

Value Description
idSets the id of an element

Return Value

A String, representing the ID of an element

The following code shows how to get the id of an element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
</head>// ww w . j  av a  2s.c om
<body>

<div id="myDIV">
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