setAttribute method

The setAttribute method allows you to change the value of a given attribute of a tag.

Syntax

javascriptelement.setAttribute(attribute name, new value);

Example

Let's change the value of value attribute of an element:

html<input id="elem" value="abcde">
javascriptlet elem = document.querySelector('#elem');
elem.setAttribute('value', '!!!');

The HTML code will look like this:

html<input id="elem" value="!!!">

See also