Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get buttons inside a div only? no JQuery

Tags:

javascript

<div class="keys">
    <button id="a"></button>
    <button id="b"></button>
</div>

I have a lot of buttons and I only want to get the ones inside the <div> with class="keys", but I can't get it to work, so far I tried:

content = document.getElementsByClassName("keys");
kbButtons = content.getElementsByTagName("button");

and I just get undefined

like image 774
y0ruba Avatar asked Sep 29 '13 23:09

y0ruba


1 Answers

Notice how the method is named "getElements...", plural.

document.getElementsByClassName() returns an HTMLCollection, an array-like object.

content = document.getElementsByClassName("keys")[0];
kbButtons = content.getElementsByTagName("button");

You can access the first element of the HTMLCollection with the [0] bracket syntax.

like image 176
Jackson Avatar answered Sep 28 '22 06:09

Jackson