panthro
panthro

Reputation: 24061

Use a selector to get a child element?

I can use the following to get an element:

document.getElementById('the-container');

Is there a way to get a child (not necessarily an immediate child) of this component.

<div id="the-container">
    <div class="whatever">
       <form>
           .....

I'd like to get the form element, without adding an id/class or targeting it via the form tag (might be other forms on page).

No jquery but happy for HTML5.

Upvotes: 0

Views: 39

Answers (1)

Quentin
Quentin

Reputation: 943561

querySelector is a property of Element objects and takes a selector as its first argument.

document.getElementById('the-container').querySelector("form");

You could also use it directly on the whole document with a more elaborate selector:

document.querySelector('#the-container form')

Upvotes: 1

Related Questions