Categories
JavaScript Answers

How to set custom attribute using JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *