Element id Property - Javascript DOM

Javascript examples for DOM:Element id


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>
</head>// ww w . j  av a  2s.c om

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

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

function myFunction() {
    var x = document.getElementsByTagName("DIV")[0];

    if (x.id === "myDIV") {
        x.style.fontSize = "30px";


Related Tutorials