Software Developer

Set Style Attribute For Multiple Elements of a Class With JavaScript

This is how to use the .setAttribute() JavaScript method to apply to multiple elements on a page. This uses pure JavaScript, no jQuery or other libraries. In this example, we are targeting all elements with the class "my-class". This will add an inline style of "cursor:pointer" to all the elements with the ".my-class" class.

(function() {
   
var titles = document.querySelectorAll(".my-class");
var i = titles.length;
while (i--) {
    titles[i].setAttribute("style", "cursor:pointer");
}

})();

By

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]