HTML DOM getElementById()Method

Document 对象参考手册Document Object

Examples

Returns the element with the specified ID:

document.getElementById("demo");

Try it Yourself »

Definition and Usage

The getElementById() method returns a reference to the first object with the specified ID.

The HTML DOM defines several methods for finding elements. Besides getElementById(), there are also getElementsByName() and getElementsByTagName().

If there is no element with the specified ID, returnnull

If multiple elements with the specified ID exist, the first one is returned.

If you need to find elements without an ID, you can consider using CSS selectorsquerySelector()。


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the getElementById() method


Syntax

document.getElementById(elementID)

Parameters

Parameters Type Description
elementID String Required. The element's ID attribute value.

Return Value

Type Description
Element object The element with the specified ID

Technical Details

DOM Version Core Level 2 Document Object


Document 对象参考手册Document Object

Other Extensions