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.
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.
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;