Easy Tutorial
❮ Obj Console Prop Param Value ❯

HTML DOM getElementById() Method

Document Object

Example

Return the element with the specified ID:

document.getElementById("demo");

Definition and Usage

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

The HTML DOM defines various methods for finding elements, besides getElementById(), such as getElementsByName() and getElementsByTagName().

If no element with the specified ID is found, it returns null.

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

If you need to find elements without an ID, you can consider using the querySelector() method with CSS selectors.


Browser Support

All major browsers support the getElementById() method.


Syntax

Parameters

Parameter 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

❮ Obj Console Prop Param Value ❯