Sometimes, we want to set custom attribute using JavaScript.
In this article, we’ll look at how to set custom attribute using JavaScript.
How to set custom attribute using JavaScript?
To set custom attribute using JavaScript, we can use the setAttribute
method.
For instance, we write
document
.getElementById("item1")
.setAttribute(
"data",
"icon: 'base2.gif', url: 'output.htm', target: 'AccessPage', output: '1'"
);
to select the element with ID item1
with getElementById
.
Then we call setAttribute
with the attribute name and value to set the attribute.
Conclusion
To set custom attribute using JavaScript, we can use the setAttribute
method.