john john - 1 month ago 10
Javascript Question

Select multiple children from within multiple children in a <ul>

I have a list structure that looks as follows:

<ul id="BirdMother" class="parent">
<ul class="child">
<li class="baby">
</ul>
<ul class="child">
<li class="baby">
</ul>
<ul class="child">
<li class="baby">
</ul>
</ul>


I want to select all the
baby
list items in an iterative manner to use in a function that looks like this:
function feedBabies(babyList)
.

How can I select all the
baby
items?

Edit: What if there are multiple baby items in the page and I only want the babies from a certain parent list. Eg: Only Bird babies. (Edited the code above).

Answer

Just use class selector

var babyList = $(".baby");

feedBabies(babyList);

For certain parent

var babyList = $("#BirdMother .baby");

feedBabies(babyList);