Home

getElementById

getElementById is a method of the Document interface in the Document Object Model (DOM). It returns a reference to the element whose id attribute matches the provided string, or null if no such element exists. The id value is expected to be unique within a document.

Usage is straightforward: var el = document.getElementById('myId'); if (el) { el.textContent = 'Hello'; } This pattern is common for accessing

Notes and behavior: getElementById searches the current document for an element with the given id. Because

Compatibility and alternatives: The method is supported by all major browsers. For more flexible selection, developers

See also: querySelector, getElementsByTagName.

a
specific
element
to
read
or
modify
its
properties
or
content.
IDs
are
intended
to
be
unique,
the
method
returns
at
most
one
element.
The
result
is
a
direct
reference
to
the
element
in
the
DOM;
if
the
element
is
removed
or
its
id
is
changed
after
retrieval,
subsequent
DOM
operations
may
reflect
that
change.
may
use
querySelector('#myId'),
which
accepts
CSS
selectors
but
can
be
slower
in
tight
loops.
getElementById
remains
a
fast
and
reliable
way
to
access
a
single
element
by
its
id.