Get the character count of a textarea including newlines

Posted by styfle on Stack Overflow See other posts from Stack Overflow or by styfle
Published on 2012-10-16T22:43:22Z Indexed on 2012/10/16 23:00 UTC
Read the original article Hit count: 255

Filed under:
|

I tested this code in Chrome and there seems to be a bug involving the newlines. I am reaching the maxlength before I actually use all the characters.

<textarea id="myText" maxlength="200" style="width:70%;height:200px">
    Lorem Ipsum is simply dummy text of the printing and typesetting industry.
    Lorem Ipsum has been the industry's standard dummy text ever since the 1500s...
    Enter something:
</textarea>

<div>
    Char Count <span id="count"></span>/<span id="max"></span>
</div>?
<script>
var ta = document.getElementById('myText');

document.getElementById('max').innerHTML = ta.maxLength;

setInterval(function() {
    document.getElementById('count').innerHTML = ta.value.length;
}, 250);?
</script>

How can I accurately get the char count of a textarea?

jsFiddle demo: http://jsfiddle.net/Qw6vz/1/

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about textarea