user3753641
user3753641

Reputation: 29

Why does this line of code in jQuery use li?

In this code why does it use children('li') because when you select children it obviously selects all the children.

$('ul.emphais').children('li').eq(3).prev().text('added with jQuery');

Upvotes: 0

Views: 47

Answers (1)

Nikhil Talreja
Nikhil Talreja

Reputation: 2774

Since ul should ideally contain only li as children, children('li') will have same meaning as children() ideally.

But if you have something like this which is still allowed syntactically (not recommended), it means different:

<ul id="list">
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <div>4<div>
</ul> 

alert($("#list").children("li").length); //Alerts 3 with and without the <div>
alert($("#list").children().length); //Alerts 4 and 3 with and without the <div>

http://jsfiddle.net/n3rTL/

Upvotes: 1

Related Questions