English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM getElementById() method

HTML DOM Document Object

getElementById()The method can obtain an element node object based on the element's id attribute, and this method is available in all browsers.

Since if an element ID is specified, it must be unique, making them an effective method for quick access to specific elements.

If you need to access an element without an ID, you can usequerySelector()Through anySelectorFind the element.

Syntax:

document.getElementById(ID)
document.getElementById('para');
Test See‹/›

Browser compatibility

All browsers fully support the getElementById() method:

Method
getElementById()IsIsIsIsIs

Parameter value

ParameterDescription
IDThe ID of the element to be located. ID is a case-sensitive string that is unique in the document. Only one element can have any given ID.

Technical Details

Return Value:Description of the DOM element object that matches the specified IDElementobject; if the matching element is not found in the document, it isnull.
DOM Version:DOM 2Level

More Examples

Get the element with id="para" and change its color:

document.getElementById('para').style.color = "blue";
Test See‹/›

Get the element with id="para" and change its font size:

document.getElementById('para').style.fontSize = ""3em";
Test See‹/›

Related References

CSS Tutorial:CSS Syntax

CSS Reference:CSS #idSelector

DOM Document querySelector()Methods

of DOM DocumentgetElementsByClassName()Methods

of DOM DocumentgetElementsByTagName()Methods

HTML DOM Document Object