Chris Waszczuk Chris Waszczuk - 7 months ago 18
HTML Question

Insert After specific child of Parent jQuery

I have a set of elements within a parent that i'm trying to insert another element into. I'm getting the amount of elements using .children().length. This works according to plan but i don't know what to look for when trying to insert after a specific element. I'm assuming i can use insertAfter() when i have the element, but how do i go about cycling through the children elements to reach the correct one.

JS

var middleCount = ($(".parent").children().length) / 2;


HTML

<div class = "parent">
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div>

Answer

You can use .eq()

var midElement = $(".parent").children().eq(middleCount);