One-hour character counter

1

I would like to know how to count how many characters contain within this h2, which in this case ex: <h2 class="h2">Ola Mundo</h2> contains 9 characters.

    
asked by anonymous 30.06.2016 / 17:47

1 answer

1

In JavaScript the Strings have a .length property that gives you exactly what you want.

DOM objects have a innerHTML property that gives their content (or innerText if you want to ignore HTML).

Combining this with a method that fetches the object from the DOM has what you want:

var length = document.querySelector('h2.h2').innerHTML.length;

Example: link

    
30.06.2016 / 17:51