187 of 264 menu

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

byenru