Animations

API

Arrays

Async

Basics

Challenges

Classes

Console

Dates

Debugging

DOM Elements

DOM Methods

DOM Navigation

DOM Properties

Event Listeners

Flow Control

Forms

Functions

Global Functions

JSON

Keywords

Libraries (3rd party)

Math

Modules

Objects

Snippets

String

Types

Widgets

Window Object

JavaScript - Get Element By Tag Name

When you use the getElementByTagName() method, will get a list(array) of all the elements of that tag TYPE. You're not looking for elements with that particular name attribute but instead, you are looking for those element types. The snippet below will not find items with a name attribute of p but instead, it will return all p elements.


I like punch and pie. This is the 3rd paragraph on this page.

Operation Dark Shield. This is the 4th paragraph on this page.



let pList = document.getElementsByTagName("p");
let results = "";

results = "The 3rd paragraph (index 2) is: " + pList[2].innerHTML + "<br />";
results += "The 4th paragraph (index 3) is: " + pList[3].innerHTML + "<br />";
document.getElementById("demo1").innerHTML = results;