views:

127

answers:

1

How to calculate the characters(spaces are excluded) within a pair of tags in JQuery or Javascript? What function?

For example:

<pre id="mytext">This is the text.      This is the text.</pre>

How to know how many characters or words in $("#mytext")?

+7  A: 

Like this:

var chars = $('#mytext').text().replace(/ /g, '').length;

If you want to exclude any whitespace, not just spaces:

var chars = $('#mytext').text().replace(/\s/g, '').length;
Greg