$(someelement).text().length and counting character encodings?

javascript, jquery

Solution

`length` is going to count `&` and similar characters as a single character. What may be causing the confusing is that you will also get whitespace before and after your string. If you trim it, you'll see that it is correct.

http://jsfiddle.net/u3byj/

$.trim($(somediv).text()).length

Problem

Lets say I have this. ``` <div id="somediv"> hello, my friend&#39;s &amp; dog are good friends! </div> ``` I think that `$(somediv).text().length` is counting the encodings and not the rendered view. I am doing some truncating on a string if it's too long, and because some of the strings (dynamic) could have `&amp;` (instead of & ), how do I properly count the total text in a `div` in its rendered view, not encoded view?

Original source

Related problems