Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

While "<li>" <= 20 add empty "<li>"

I'm trying to add a <li> inside an <ul> if the <li> count is less or equal to 20, but the code doesn't seem to be working...

I'm using the following jQuery:

while ($(".mosaico > li").length() <= 20) {
   $('.mosaico').append('<li>test</li>');
};

And nothing seems to happen... The HTML is:

<ul class="mosaico">
    <li>
    </li>

    <li>
    </li>

    <li>
    </li>
</ul>
like image 244
Tiago Avatar asked Dec 07 '25 12:12

Tiago


1 Answers

.length is a property, not a function.

But you shouldn't run the selector in a loop like that. Just calculate how many you need, and add them all at once.

var mosaico = $(".mosaico"),
    len = 20 - mosaico.children('li').length,
    str = '';

while( len-- > 0 ) {
    str += '<li>test</li>';
}

mosaico.append(str);

http://jsfiddle.net/pQXNT/2

like image 120
RightSaidFred Avatar answered Dec 10 '25 00:12

RightSaidFred