Quick Word Count Listing on Drupal Post

If you're writing something in drupal and you need to know the word count here's a quick snippet of jQuery code that you can run that puts a little word count indicator before the textarea where you're writing the post. And yes, I'm attempting to procrastinate while writing a 1000 word philosophy essay.

jQuery("#edit-body").keyup(function() { 
    var t; 
    if (jQuery("#edit-body-count").length == 0) {
        t = jQuery('<p id="edit-body-count"></p>'); 
        jQuery(this).before(t);
    } else {
        t = jQuery("#edit-body-count");
    }
    t.html(jQuery.trim(jQuery(this).val()).split(/\s+/).length + " word(s)"); 
});

Update: A few people have contacted me wanting to know where this code would go. Well, it wouldn't really go anywhere as it is, because it's a bit of code you'd execute yourself after the page has loaded. However, if you wrap it all in a jQuery(function() { /*code here*/ }); then it could be placed anywhere on your site. Whether that's you creating a custom drupal plugin that does nothing other than call "drupal_add_js" and add this script to the page, or you modify your site's template theme so that it's hardwired directly into your site, or some other more clever option of which I'm unaware. Either way, the code's position is irrelevant if you wrap it in a jQuery onready event as I've shown above, only that it is on the page.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

Captcha
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
18 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.