Finding DOM Objects by Class Name

var objs = document.getElementsByClassName("myClass"); for (var i=0; i<objs.length; i++){   var htmlElement = objs[i];   ... }

You can also search for HTML elements by their class attribute using the document.getElementsByClassName(class). This function returns an array of DOM objects with matching class attributes. You can then iterate over that list using a JavaScript loop and apply changes to each DOM element.

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.