html - How to find the parent id's of an h1 tag array in Javascript? -
i have in html:
<div id="div0"></div> <div id="div1"><h1>h1(0)</h1></div> <div id="div2"><h1>h1(1)</h1></div> <div id="div3"><h1 class="center">h1(2)</h1></div> <div id="div4"><h1>h1(3)</h1><h1>h1(4)</h1></div>
my javascript:
var tags=document.getelementsbytagname('h1'); console.log(tags) = htmlcollection[h1,h1,h1.center,h1,h1]
i'm trying figure out how id of parent of, say, tags[2], "div3".
this should work:
tags[2].parentnode.id;
https://developer.mozilla.org/en-us/docs/dom/node.parentnode
https://developer.mozilla.org/en-us/docs/dom/node.parentelement
Comments
Post a Comment