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 »

Description

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

The getElementById() Method

CSS Syntax

CSS #id Selector



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
StringThe 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

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.