Jquery count characters

You are getting 0 because the h1 hasnt loaded when the code has been run so firstly you need to put it inside a document.ready. This still won't give the answer you want as it will just tell you the number of h1 tags on the page. To get the answer you want you need to do:

$(document).ready(function() {
    var count = $('h1').text().length;
    alert(count);
});

try this:

$(document).ready(function(){
  var count = $('h1').text().length;
  alert(count);
})

$("h1") returns a jQuery object (in which the length property is the number of elements returned), and since you are calling it before the page is completely loaded it is actually retuning 0 elements.

What you want is:

$(document).ready(function() {
    var count = $("h1").text().length;
    alert(count);
});

Tags:

Jquery