user2665694
user2665694

Reputation:

Finding a DOM node in the subtree of a node?

I have HTML with something like

<div class="foo">
<h1>foo 1</h1>
..</div>

<div class="foo">
<h1>foo 2</h1>
..</div>

Finding the div nodes using getElementsByTagName('div') is not a problem.

While iterating of the div nodes I need to find the first H1 inside the subtree of the DIV nodes. Is there something like getElementByTagName() on a DOM node?

Upvotes: 1

Views: 3787

Answers (1)

lonesomeday
lonesomeday

Reputation: 237865

No. Use array notation to get the first element found:

var firstHeading = div.getElementsByTagName('h1')[0];

Edit If you were asking if you could use getElementsByTagName on nodes other than the document, yes you can. All DOM element nodes plus the document node have the getElementsByTagName method.

Upvotes: 4

Related Questions