Hi I want to be able to count the number of displayed characters in a Div with javascript/jquery. For example
This is my div!&l
Try this. I am trimming to avoid any white spaces in the content start or end.
$.trim($("#mydiv").text()).length
If you want to keep spaces also in the count then don't trim it just use this.
$("#mydiv").text().length
In case you are searching for a Vanilla Javascript solution.
Here is one with whitespace:
document.querySelectorAll('#mydiv')[0].textContent.length
Here is one without whitespace:
document.querySelectorAll('#mydiv')[0].textContent.replace(/\s/g,'').length
Sample
http://jsfiddle.net/TrMRB/
$("#mydiv p").text().length;
$('#mydiv').text().length
should do the trick.