jQuery count characters and add class

css, html, jquery

Solution

You could do something like this:

// The number of characters
var XX = 100;

$('li').filter(function() {
    return $(this).text().length > XX;
}).addClass('someClass');

Problem

Is it possible to use jQuery to count how many characters are in, for example, an `<li>`, and if the amount is greater than XX characters, apply a class to that element? I've seen plenty of jQuery character counters (ie http://plugins.jquery.com/plugin-tags/character-counter), and I can see how `.addClass()` would handle that part of things, but am having some trouble putting it together. Any pointers gratefully received. Many thanks THANKS EVERYONE FOR THE ANSWERS - Found one solution that works for me as indicated below

Original source