var txt = $('.content')[0].text() , charCount = txt.length , wordCount = txt.replace( /[^\w ]/g, "" ).split( /\s+/ ).length ; $( '#somwhereInYourDocument' ).text( "The text had " + charCount + " characters and " + wordCount +" words" );
Run replace before split to get rid of punctuation, and run split with a regex to process newlines, tabs, and a few spaces between words.
EDIT added a bit of text (...) to write to node, as the OP indicated in the comment to another answer.
EDIT , you still need to wrap it in a function to make it work after the page loads
$( function(){ var txt = $('.content')[0].text() , charCount = txt.length , wordCount = txt.replace( /[^\w ]/g, "" ).split( /\s+/ ).length ; $( '#somwhereInYourDocument' ).text( "The text had " + charCount + " characters and " + wordCount +" words" ); });
Otherwise, it starts before everything is displayed on the page.
source share