Click here to Skip to main content
Licence 
First Posted 7 Sep 2006
Views 20,554
Bookmarked 14 times

Javascript based Word Counter for text entered in a Text area, works for poastback also.

By | 7 Sep 2006 | Article
Gives an idea about counting words entred in a text area, this will works fine whenever postback occurs also, implemented in javascript and codebehind(small portion).

Introduction

This article gives an idea about how to implement count of the words entered in a text area and stores the value of the count in an hidden variable and used whenever postbak occurs.

The javasript method wordcounter() will be called from textarea's onkeypress and onkeydown events. For postback events this method will be called from body's onload event.

The lines of javascript code written are given below.

function wordCounter(chkBodyLoad)

{

 if(chkBodyLoad == 0)

    {

        var wordcounter=0;

        var textField = event.srcElement;

// Counts the spaces and new line characters while ignoring double spaces, usually one in between each word.

        for (x=0; x < textField.value.length; x++)

        {

        if ((textField.value.charAt(x) == " " && textField.value.charAt(x-1) != " ")||textField.value.charAt(x) == "\n" && textField.value.charAt(x-1) != "\n"))

                {

                        wordcounter = wordcounter + 1;

                }

        }

        document.forms[0].hidWordCount.value = wordcounter;

        spnWordCount.innerText = wordcounter;

    }

    else if(chkBodyLoad == 1) 
    {

        spnWordCount.innerText = document.forms[0].hidWordCount.value ;

    }

}

"hidWordCount" is used for storing the word count whenever the page is submitted.

<form id=frmWordCounter runat="server">

And the method calls are given below,

onKeyDown="wordCounter(0);"

onkeyup="wordCounter(0);"
method call from body is onload="wordCounter(1);"

</form>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

d.mohana

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralYour source code zip file is missing PinmemberMeDeeSa4:20 14 Sep '06  
Generalone liner with different regex than mzeo77 [modified] PinmemberDerek Read14:19 11 Sep '06  
GeneralWithout the loop Pinmembermzeo7722:24 7 Sep '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 8 Sep 2006
Article Copyright 2006 by d.mohana
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid