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

后端 未结 4 1416
鱼传尺愫
鱼传尺愫 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:07

    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
    

提交回复
热议问题