.setAttribute()
void Element.setAttribute(String name, String value)
Set the value of an attribute.
element.setAttribute(name, value);
Sets the attribute name of element to value.
Note:
Attributes differ from properties on an element. A property is simply a property on the object, which can hold any type, while an attribute is always a string. For a variety of interactive properties, such as
checked
or value
of an input, you probably want to simply set the property, eg. checkboxElement.checked = true;
or inputElement.value = newValue;
.
See also: .getAttribute()
Example
HTML:
<input type="checkbox">
JavaScript:
let value = document.query("input").setAttribute("type", "text");
Resulting HTML:
<input type="text">