Latest web development tutorials

HTML DOM getElementById () method

Document Object Reference Document Object

Examples

Returns the ID of the element:

document.getElementById("demo");

try it"

Definition and Usage

getElementById () method returns the specified ID to the owner of the first object.

HTML DOM defines a variety of methods to find elements in addition to getElementById (), as well as getElementsByName () and getElementsByTagName ().

If no ID or null elements

If more of the specified ID exists an element of return undefined.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support getElementById () method


grammar

document.getElementById( elementID )

Parameter s

参数 类型 描述
elementID String 必须。元素ID属性值。

return value

类型 描述
元素对象 指定ID的元素

technical details

DOM version Core Level 2 Document Object


Document Object Reference Document Object