Gurebu Bokofu Gurebu Bokofu - 5 months ago 8
Javascript Question

JavaScript: how to get the contains of the DOM w/o ID and class?

Is it possible to get only one DOM contains by tag name without ID and class?

As you know, in HTML5

<main>
,
<header>
and other new tags has been added, and thanks to it, to define the style of header or footer, assigning the class or id line
<div class="header">
becomes unnecessary.

E. g. I need to get the
<header>
contains, like

var test = document.getElementById("test");


by don't want to assign ID or class to header (nothing difference between
<div id="header">
and
<header id="header">
. However

var test = document.getElementsByTagName("header") ;


will not work (I mean will not save the
<header>
contains in variable value) even if there is only one header in the page.

Anything that I can to do?

Answer
document.getElementsByTagName('header') 

will store all the header which is in the page in array.

var test = document.getElementsByTagName("header");
alert(test[0].innerHTML);
alert(test[1].innerHTML);
<header>first test</header>
<header>second test</header>

Reference link here