Rahul Singh
Rahul Singh

Reputation: 2062

Jquery nth-child shows undefined for index 1

The html code is as follows:

<div id="customer-projects">
      <h3 class="lead"> Projects </h3>
      <ul class="project-list">
        .
        .
      </ul>
      .
      .
      <ul class="project-list">
        .
        .
      </ul> 
<div>

Is there any particular reason why $('#customer-projects .project-list:nth-child(1)').html() returns 'undefined' and not the contents of the first ul.

Upvotes: 1

Views: 927

Answers (3)

Rohan Kumar
Rohan Kumar

Reputation: 40639

Use eq() like

$('#customer-projects .project-list:eq(0)').html(); //indexing starts from 0

Demo

Upvotes: 1

Sergei Gorjunov
Sergei Gorjunov

Reputation: 1799

Try :eq(1) instead of :nth-child(1)

Upvotes: 0

Mr. Alien
Mr. Alien

Reputation: 157334

Modify your selector like this, cuz the issue is, you are using nth-child(1) which will match h3 element and not the ul.project-list, so it will return undefined, use nth-of-type(1) with element.class selector.

console.log($('#customer-projects ul.project-list:nth-of-type(1)').html());

Demo

Upvotes: 0

Related Questions