English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM id attribute

HTML DOM Element Object

IDSet or return the id attribute value of an element.

The ID should be unique within the page and is usually used todocument.getElementById()The method returns the element.

Syntax:

Return the id attribute:

element.id

Set the id attribute:

element.id = id
var x = document.getElementsByTagName("li")[0].id;
Test See‹/›

Browser compatibility

All browsers fully support the id attribute:

Properties
idIsIsIsIsIs

Attribute value

ValueDescription
idID of the specified element

Technical Details

Return Value:A string representing the element's ID attribute
DOM Version:DOM Level1

More Examples

Change Element ID:

document.getElementsByTagName("li")[0].id = "myList2";
Test See‹/›

Check if the ID of the first <div> element in the document is "myDiv":

var x = document.querySelector("div");
if (x.id == "myDiv") {
   alert("Yes... The DIV has 'myDiv' ID");
} else {
   alert("False");
}
Test See‹/›

Related References

CSS Tutorial:CSS Syntax

CSS Reference:CSS #idSelectors

HTML Reference:HTML GlobalidProperties

HTML DOM Reference:HTML DOM getElementById() Method

HTML DOM Element Object