XML DOM getElementById() Method

Document Object Reference Manual

Definition and Usage

The getElementById() method finds the element with the specified unique ID.

Syntax:

getElementById(elementid)
Parameter Description
elementid The id attribute value of the element you want to get.

Return value

Element node representing the document element with the specified id attribute. If no such element is found, null is returned.

Description

This method will retrieve the id attribute value of elementid element node and return it. If no such element is found, null is returned. The value of the id attribute is unique in the document, if this method finds more than one with the specified elementid element node, it will randomly return such an element node or return null.

This is an important commonly used method, as it provides a convenient way to obtain the Element object representing the specified document element.

Note:The name of this method ends with Id, not ID, do not spell it wrong.

In an HTML document, this method always retrieves the attribute with the specified id. You can use HTMLDocument.getElementByName() Method, finding HTML elements based on the value of their name attribute.

In an XML document, this method uses any attribute of type id to find, regardless of the name of the attribute. If the type of the XML attribute is unknown (for example, the XML parser ignores or cannot locate the DTD of the document), this method always returns null. In client-side JavaScript, this method is not often used with XML documents. In fact, getElementById() was originally defined as a member of the HTMLDocument interface, but was later moved into the Document interface in the 2nd level DOM.

See Also

Document.getElementsByTagName()

Element.getElementsByTagName()

HTMLDocument.getElementByName()

Document Object Reference Manual