Hi I want to be able to count the number of displayed characters in a Div with javascript/jquery. For example
<div id=mydiv>
<p>This is my div!</p>
</div>
I want to get the number 15 since that's how many chars in the div including spaces.
Thanks!
In JavaScript, we can count the string occurrence in a string by counting the number of times the string present in the string. JavaScript provides a function match(), which is used to generate all the occurrences of a string in an array.
To count all HTML elements, we use length property. The length property is used to count number of the elements of the jQuery object.
$('#mydiv').text().length
should do the trick.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With