Definition and Usage
The setAttribute() function sets an attribute value on a DOM element.
Features
Syntax
element.setAttribute(attributeName, value)
Parameters
Return Value
None (undefined).
To get the value of an attribute from a DOM element, use the getAttribute() function.
To remove a specific attribute from a DOM element, use the removeAttribute() function.
Examples
Let's look at some examples for different situations.
If the Specified Attribute Already Exists
If the specified attribute already exists on the element, its value is updated.
<input type="password" value="test">
<button type="button">Show Password</button>
const elem = document.querySelector("input");
const button = document.querySelector("button");
button.addEventListener("click", () => { // Button click event
/* When the button is clicked,
the input element’s type attribute value is changed from password to text. */
elem.setAttribute("type", "text");
});
Code Explanation
The querySelector() function returns the first element that matches the specified CSS selector within the HTML document or a specific element.
"Show Password" button!
If the Specified Attribute Does Not Exist
<input type="text">
<button type="button">Add Test Value</button>
const elem = document.querySelector("input");
const button = document.querySelector("button");
button.addEventListener("click", () => { // Button click event
/* When the button is clicked,
the input element does not have a value attribute,
but a value attribute is added and set to test. */
elem.setAttribute("value", "test");
});
"Add Test Value" button!
Setting Multiple Attributes
Each attribute is set by calling the setAttribute() function separately.
const element = document.getElementById("example-id");
// Set first attribute
element.setAttribute("alt", "Set your alt");
// Set second attribute
element.setAttribute("title", "Set your title");
// Set third attribute
element.setAttribute("src", "/images/image.jpg");
In the Case of Boolean Attributes
HTML attributes that do not require a value are called Boolean attributes. For example, hidden, open, and autoplay are Boolean attributes.
Boolean attributes do not require a value, but once they are set, the attribute is applied regardless of the value. What matters is the existence of the attribute, not its actual value. The two parameters of the setAttribute(attributeName, value) function are required, so a value must be provided for the attribute. In this case, it is recommended to use an empty string ("").
<input type="text">
<button type="button">Hide Input Box</button>
const elem = document.querySelector("input");
const button = document.querySelector("button");
button.addEventListener("click", () => { // Button click event
/* When the button is clicked,
the hidden attribute is added to the input element.
The value is set to an empty string (""). */
elem.setAttribute("hidden", "");
});
"Hide Input Box" button!
Things to Keep in Mind
element.setAttribute("style", "background-color:blue;");
Instead, use the JavaScript style property.
element.style.backgroundColor = "blue";
Browser Compatibility
| Method |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
setAttribute()
|
1 | 12 | 1 | 1 |
Specifications
| Specification | |
|---|---|
setAttribute()
|
DOM Standard #ref-for-dom-element-setattribute① |
See Also
- JavaScript getAttribute() Function – Getting Attribute Values from Elements
- JavaScript removeAttribute() Function – Removing Specified Attributes from Elements
- JavaScript hasAttribute() Function – Checking If an Element Has an Attribute
- JavaScript toggleAttribute() Function – Toggling a Boolean Attribute on an Element
- JavaScript classList.add() – Add Class Values to an Element