Summary: in this tutorial, you will learn how to set the value of an attribute on a DOM element using the setAttribute() method.
To set a value of an attribute on an element, you use the setAttribute() method:
element.setAttribute(name,value);
For example, to set the title attribute of the anchor element, you use the following code:
let link = document.querySelector('a');
link.setAttribute('title','JavaScript DOM setAttribute');
You can also use the setAttribute()method to set the value for a data-* attribute. For example:
let btn = document.querySelector('a');
btn.setAttribute('data-method','post');
Thank you for your feedback!