Home

getAttribute

getAttribute is a method available on DOM Element objects that retrieves the value of a specified HTML attribute as a string. It accepts the attribute’s name as an argument and returns the attribute’s value, or null if the attribute is not present on the element.

The value returned by getAttribute is the raw value as it appears in the HTML markup. This

There is also getAttributeNS for attributes that reside in a specific namespace, and getAttributeNode for retrieving

Typical usage examples include reading attributes defined in HTML, such as a link’s href, data attributes, or

differs
from
a
property
of
the
element
(for
example,
element.href
or
element.src),
which
may
reflect
a
processed
or
resolved
value.
For
boolean
attributes
such
as
disabled
or
checked,
the
attribute
can
be
present
without
a
value
in
markup;
in
that
case
getAttribute
returns
an
empty
string,
while
null
indicates
the
attribute
is
absent.
an
Attr
node.
In
contrast
to
getAttribute,
properties
of
elements
often
reflect
the
current
state
or
computed
values
and
may
not
correspond
to
the
attribute’s
original
string.
custom
attributes.
Example:
var
hrefValue
=
document.querySelector('a').getAttribute('href');
If
the
attribute
is
not
present,
this
returns
null.
When
deeper
processing
is
needed,
such
as
obtaining
fully
resolved
URLs,
the
corresponding
property
(e.g.,
element.href)
may
be
used
instead
of
getAttribute.