HTML DOM Element id
Examples
Get the id of the first anchor:
let id = document.getElementsByTagName("a")[0].id;
Try it Yourself »
Change the id of an element:
document.getElementById("demo").id = "newid";
Try it Yourself »
Change the font size of "myP":
const element = document.getElementById("myP");
element.style.fontSize = "30px";
Try it Yourself »
Definition and Usage
The id
property sets or returns the value of an element's id attribute.
Note
An id should be unique within a page.
See Also
Syntax
Return the id property:
element.id
Set the id property:
element.id = id
Property Value
Value | Description |
id | The id of the element. |
Return Value
Type | Description |
String | The id of the element. |
Browser Support
element.id
is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |