Stephanie
Stephanie

Reputation: 65

jQuery: If character limit is "x" do "this"?

I'm trying to set a class on a div where there's text inside, but I would like to add a class only if there's a character count of 60.

So something like, "if character count = 60 {" and then I'll add the class. Is this possible?

Upvotes: 1

Views: 56

Answers (1)

charlietfl
charlietfl

Reputation: 171698

Can use jQuery filter() :

$(selector).filter(function(){
   return $(this).text().length >= 60; 
}).addClass('someClass');

filter() will reduce the elements defined in selector to subset ( or none) that match conditions returned from the callback

DEMO

Upvotes: 6

Related Questions