Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How do I work out in jQuery a character counter of a p/div tag?

Basically i want to show a different css value IF the character value >=50.

Been struggling for hours with it :)

like image 319
Adi Avatar asked Feb 25 '11 17:02

Adi


2 Answers

Use

$("#myDiv").text().length;
like image 169
Anurag Avatar answered Sep 20 '22 02:09

Anurag


var $div = $('#mydiv');
if($div.text().length >= 50) {
    $div.addClass('class');
}
like image 44
jevakallio Avatar answered Sep 22 '22 02:09

jevakallio