Nishant Nawarkhede
Nishant Nawarkhede

Reputation: 8400

Get length of text in div using div

I have div with fixed width of 30%. I want to get the length of text in that div.

Here is my fiddle

What i am doing here is ,

var text=$('#test').text();
text=text.trim();
var len=text.length;
alert(text);
alert(len);

I am getting length of only text . How can i get length of text after applying html tags to it.

Current behaviour : Getting length as 105.
Expected behaviour : 29 * total line
[ 29 is atmost characters can be in line of div ]

Update

I also want to to count the length including the area (green) shown in following image.
enter image description here

Upvotes: 2

Views: 11677

Answers (2)

semirturgay
semirturgay

Reputation: 4201

use .html() instead of .text() to count also <br> tags DEMO

Upvotes: 2

Aamir Afridi
Aamir Afridi

Reputation: 6411

http://jsfiddle.net/aamir/4M8V7/7/

//With html length
console.log( $.trim($('#test').html()).length );

//Without html length
console.log( $.trim($('#test').text()).length );

Upvotes: 3

Related Questions