HTML - attributes - id

revision:


Content

"id" attribute : specifies a unique id for an element syntax some examples


"id" attribute : specifies a unique id for an element

top

The value must be unique within the HTML document.
The "id attribute" is most used to point to a style in a style sheet, and by JavaScript (via the HTML DOM) to manipulate the element with the specific id.

The id attribute is a global attribute, and can be used on any HTML element. Its purpose is to identify the element when linking (using a fragment identifier), scripting, or styling (with CSS).

"id" attribute used as bookmark:

First, create a bookmark with the "id attribute", then add a link to the bookmark from within the same page or add a link to the bookmark from another page.

using the id attribute in JavaScript:

The "id attribute" can be used by JavaScript to perform some tasks for that specific element. JavaScript can access an element with a specific id with the getElementById() method.


syntax

top

<element id=" "></element>

The syntax for "id" in CSS: write a hash character (#), followed by an id name. Then, define the CSS properties within curly braces {}.


some examples

top

Hello World!

codes:
                    <h4 id="myHeader">Hello World!</h4>
                    <button style="margin-left:3vw;" onclick="changeText()">Change text</button>
                    <script>
                        function changeText() {
                        document.getElementById("myHeader").innerHTML = "Have a nice day!";
                        }
                    </script>
                

Jump to top

codes:
                    <h1 id="top">HTML - attributes - id</h1>
                    <p class="spec"><a href="#top">Jump to top</p>