Count the characters in a paragraph using jQuery (* not * for input / textarea)

How can I handle p / div character counter in jQuery?

Basically, I want to show a different css value if the character value is> 50.

He fought the clock with him :)

+8
javascript jquery html
source share
3 answers

Using

$("#myDiv").text().length; 
+13
source share
 var $div = $('#mydiv'); if($div.text().length >= 50) { $div.addClass('class'); } 
+6
source share

Put a class "long" for all div and p elements with more than 50 characters:

 $("p, div").filter(function(){ return $(this).text().length >=50; }).addClass('long'); 

If you do not know how much content you have, then, presumably, this content is generated dynamically by the server, right? And if so, it does not make sense to have a server that knows how much content it will cram into these containers - add a class dynamically when creating a page to send? Why rely on jQuery?

+4
source share

All Articles