setAttribute method
The setAttribute
method allows
you to change the value of a given
attribute of a tag.
Syntax
element.setAttribute(attribute name, new value);
Example
Let's change the value of
value
attribute of an element:
<input id="elem" value="abcde">
let elem = document.querySelector('#elem');
elem.setAttribute('value', '!!!');
The HTML code will look like this:
<input id="elem" value="!!!">
See also
-
the
getAttribute
method
that gets attributes -
the
removeAttribute
method
that removes attributes -
the
hasAttribute
method
that checks attributes