JS - element properties - id

revision:


sets or returns the value of the "id" attribute of an element.

top

Syntax:

element.id : returns the id property; the id of the element.

element.id = id : sets the id property.

property value:

id : the id of the element.

example

HTML CSS JavaScript

The value of the id attribute of the first link is:

The value of the id attribute of the second link is:

code:
                <div>
                    <a id="id1" href="https://www.w3schools.com/html/">HTML</a>
                    <a id="id2" href="https://www.lwitters.com/css/">CSS</a>
                    <a id="id3" href="https://www.wikipedia.org/">JavaScript</a>
                    <p>The value of the id attribute of the first link is: <span id="prop"></span></p>
                    <p>The value of the id attribute of the second link is: <span id="prop1"></span></p>
                </div>
                <script>
                    let id = document.getElementsByTagName("a")[1].id;
                    document.getElementById("prop").innerHTML = id;
                    let id1 = document.getElementsByTagName("a")[2].id;
                    document.getElementById("prop1").innerHTML = id1;
                </script>
            

Click "Change" to change my font size.

            <div>
                <button onclick="firstFunction()">Change</button>
                <p id="id4">Click "Change" to change my font size.</p>
            </div>
            <script>
                function firstFunction() {
                    const element = document.getElementById("id4");
                    element.style.fontSize = "24px";
                }
            </script>