HTML DOM Element id Attribute

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:

getElementById() Method

CSS Syntax

CSS #id Selector

Example

Example 1

Get the id of the first anchor:

let id = document.getElementsByTagName("a")[0].id;

Try it yourself

Example 2

Change the element's id:

document.getElementById("demo").id = "newid";

Try it yourself

Example 3

Change the font size of "myP":

const element = document.getElementById("myP");
element.style.fontSize = "30px";

Try it yourself

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