How to count the number of characters in a DIV with javascript

后端 未结 4 1411
鱼传尺愫
鱼传尺愫 2021-01-03 22:31

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

4条回答
  •  时光取名叫无心
    2021-01-03 23:06

    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
    

提交回复
热议问题