tags:

views:

37

answers:

1

If a href is longer than 40 chars I want apply a class to the previous LI element.

How can I do this in jQeury?

+4  A: 

Try this:

$('li a').filter(function () {
    return $(this).attr('href').length > 40;
}).closest('li').addClass(...);

Demo here: http://jsfiddle.net/BPQBe/1

Further reading:

Matt
Verified: http://jsfiddle.net/BPQBe/
bdukes
@bdukes +1 thank you
Matt
You don't need to use the `each` method, you can directly work on the result set from the `filter` call (I've updated my jsfiddle demo with an example).
bdukes
@bdukes nice catch, I've updated my answer.
Matt