Name

Document.getElementById( ): find an element with the specified unique ID — DOM Level 2 Core:

Synopsis

Element getElementById(StringelementId);

Arguments

elementId

The value of the id attribute of the desired element.

Returns

The Element node that represents the document element with the specified id attribute or null if no such element is found.

Description

This method searches the document for an Element node with an id attribute whose value is elementId and returns that Element. If no such Element is found, it returns null. The value of the id attribute is intended to be unique within a document, and if this method finds more than one Element with the specified elementId, it may return one at random, or it may return null. This is an important and commonly used method because it provides a simple way to obtain the Element object that represents a specific document element. Note that the name of this method ends with “Id”, not with “ID”; be careful not to misspell it.

In HTML documents, this method searches for an element based on the value of its id attribute. Use HTMLDocument.getElementsByName( ) to search for HTML elements based on the value of their name attributes.

In XML documents, this method performs its search using any attribute whose type is id, regardless of what the name of that attribute is. If XML attribute types are not known (because, for example, the XML parser ignored or could not locate the document’s DTD), this method always returns null. In client-side ...

Get JavaScript: The Definitive Guide, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.