Mike
Mike

Reputation: 3575

JQuery find first child gives wrong result

<dl>
    <dt>title</dt>
    <dd>first
    </dd>
    <dd>second
    </dd>
    <dd> third
    </dd>
</dl>

Only the last try give me the correct result, is this a bug or anything wrong in my code? I use jquery 1.10.1. http://jsfiddle.net/RYJ9z/

var h=$("dl").find("dd:first-child").html();
alert(h);
var h2=$("dl").find("dd:nth-child(1)").html();
alert(h2);
var h3=$("dl").find("dd").first().html();
alert(h3);

Upvotes: 0

Views: 152

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

$("dl").find("dd:first-child"): looks for an dd element which is the first child of its parent but dt element is the first child not dd

Try :first-of-type

$("dl").find("dd:nth-child(1)"): nth-child uses 1 based index so first child of the parent element is an dt element not dd so this also fails

Try :nth-of-type

Demo: Fiddle

Upvotes: 2

Related Questions