Jarred Jarred - 1 year ago 130
jQuery Question

jQuery nth-child selector not working

I have a div id (auctions), and I want to swap out the HTML with the children of auction-list. However, jQuery cannot select the children of the auction-list div.

Here's the HTML:

<div id="all">
<div id="auctions"></div>

<div id="auction-list" class="hide">
<div class="auction">Test</div>
<div class="auction">Test</div>

Here's the jQuery:


And here are the outputs for the Javascript alerts:

First Alert


Second Alert

<div class="auction">Test</div>
<div class="auction">Test</div>

Third Alert


What am I overlooking here?

Answer Source

You need a space between your selectors, like this:

alert($("#auction-list :nth-child(1)").html());
//                    ^-- Space here

With your selector, it's looking for the element #auction-list, which is a the 1st child of another element, when what you're actually looking for an element that is the nth child of the list.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download