How to use the setAttribute method in JavaScript?

In JavaScript, the setAttribute method is used to set the value of a specified element’s attribute. Its syntax is as follows:

element.setAttribute(name, value);

In this case, “name” is the property name to be set, and “value” is the property value to be set. For example, to set the class attribute value of an element to “example,” you can use it like this:

var element = document.getElementById("exampleElement");
element.setAttribute("class", "example");

By using the above example, set the class attribute of the “exampleElement” element to “example”. This will result in the corresponding changes on the page.

bannerAds