English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The HTML id global attribute defines a unique identifier (ID) for the entire document. It is used to identify elements in links (using fragments), scripts, and styles (through CSS).
Change the content of a paragraph using JavaScript script through the id attribute:
<!DOCTYPE html <html <head <meta charset="utf-8"> <title>Basic Tutorial Website(oldtoolbag.com)</title> <script function changeText() { document.getElementById("myHeader").innerHTML="Haha, I've been changed, darling!"; } </script> </head> <body <h1 id="myHeader">Hello World!/h1> <button onclick="changeText()">Dynamically change the text content of the id</button> </body> </html>Test and see ‹/›
IEFirefoxOperaChromeSafari
All mainstream browsers support the id attribute
The id attribute specifies the unique id of an HTML element.
The id must be unique in the HTML document.
The id attribute can be used as a link anchor (link anchor), to change or add styles to the element with the specified id through JavaScript (HTML DOM) or through CSS.
in HTML5 in, the id attribute can be used for any HTML element (it will validate any HTML element. But it may not be useful).
in HTML 4.01 in, the id attribute cannot be used for: <base>, <head>, <html>, <meta>, <param>, <script>, <style>, and <title>.
Note: HTML 4.01 There are strict restrictions on the value of id (For example: in HTML 4.01 The id value cannot start with a number).
<element id="id">
Value | Description |
---|---|
id | Specifies a unique id for the element. Naming rules:
|
Using the id attribute in CSS
How to use the id attribute in CSS to add styles to elements