English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
Return the id attribute:
element.id
Set the id attribute:
element.id = id
var x = document.getElementsByTagName("li")[0].id;Test See‹/›
All browsers fully support the id attribute:
Properties | |||||
id | Is | Is | Is | Is | Is |
Value | Description |
---|---|
id | ID of the specified element |
Return Value: | A string representing the element's ID attribute |
---|---|
DOM Version: | DOM Level1 |
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‹/›
CSS Tutorial:CSS Syntax
CSS Reference:CSS #idSelectors
HTML Reference:HTML GlobalidProperties
HTML DOM Reference:HTML DOM getElementById() Method