Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML DOM getElementById() method

HTML DOM getElementById() method

Document Object Reference Document Object

Example

Return the element with the specified ID:

document

.

getElementById

(

"

demo

"

)

;

Try it »

Definition and Usage

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

HTML DOM defines multiple methods for finding elements, besides getElementById(), there are also getElementsByName() and getElementsByTagName().

If no element with the specified ID is found, return null

If there are multiple elements with the specified ID, return the first one.

If you need to find elements that do not have an ID, you can consider using querySelector().


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the getElementById() method


Syntax

document.getElementById(

elementID

)

Parameters

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

Return Value

Type Description
Element object The element with the specified ID

Technical Details

DOM version Core Level 2 Document Object

Document Object Reference Document Object