HTML DOM Element id Attribute
- Previous Page hasChildNodes()
- Next Page innerHTML
- Go Up One Level HTML DOM Elements Object
Definition and Usage
id
Sets or returns the value of the element's id attribute.
Note:An id should be unique within a page.
See also:
Example
Example 1
Get the id of the first anchor:
let id = document.getElementsByTagName("a")[0].id;
Example 2
Change the element's id:
document.getElementById("demo").id = "newid";
Example 3
Change the font size of "myP":
const element = document.getElementById("myP"); element.style.fontSize = "30px";
Syntax
Return the id attribute:
element.id
Set the id attribute:
element.id = id
Attribute value
Value | Description |
---|---|
id | the element's id. |
Return value
Type | Description |
---|---|
String | the element's id. |
Browser support
All browsers support element.id
:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Support | Support | Support | Support | Support | Support |
- Previous Page hasChildNodes()
- Next Page innerHTML
- Go Up One Level HTML DOM Elements Object