jQuery: Checking for length of ul and removing an li element?

Posted by Legend on Stack Overflow See other posts from Stack Overflow or by Legend
Published on 2010-05-30T04:45:44Z Indexed on 2010/05/30 4:52 UTC
Read the original article Hit count: 365

Filed under:
|
|

I am trying to remove the last <li> element from a <ul> element only if it exceeds a particular length. For this, I am doing something like this:

var selector = "#ulelement"
if($(selector).children().length > threshold) {
   $(selector + " >:last").remove();
}

I don't like the fact that I have to use the selector twice. Is there a shorter way to do this? Something like a "remove-if-length-greater-than-threshold" idea. I was thinking that maybe there is a way to do this using the live() function but I have no idea how.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery