Finding DOM Objects by Tag Name

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

Another way search for HTML elements is by their HTML tag using the document.getElementsByTagName(tag). This function searches the document for all DOM objects that have the specified tag name and returns them in an array. You can then iterate over that array using a JavaScript loop and access or 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.