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>
.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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With