Jump to content

Word count in textarea


Panta

Recommended Posts

`   <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script><script>var maxwords = 250;//function check_length(obj, cnt, rem){    var ary = obj.value.split(" "); // doubled spaces will throw this off    var len = ary.length;    cnt.innerHTML = len;    rem.innerHTML = maxwords - len;    if (len > maxwords) {        alert("Message in '" + obj.name + "' limited to " + maxwords + " words.");        ary = ary.slice(0,maxwords-1);        obj.value = ary.join(" "); // truncate additional words        cnt.innerHTML = maxwords;        rem.innerHTML = 0;        return false;    }    return true;} </script>HTML   <textarea name="Message 1" onkeypress=" return check_length(this, document.getElementById('count1'), document.getElementById('remaining1'));"></textarea>Word count: <span id="count1">0</span>  Words remaining: <span id="remaining1">250</span><textarea name="Message 2" onkeypress=" return check_length(this, document.getElementById('count2'), document.getElementById('remaining2'));"></textarea>Word count: <span id="count2">0</span>  Words remaining: <span id="remaining2">500</span>

please my problem is that i want the different textarea to work according to its limit, but i notice that both uses same word limit. your help will be appreciated. thanks in advance

Link to comment
Share on other sites

You want each textarea to have its own limit?

 

Inside the event handler you could start it off like this:

if(obj.name == "Message 1") {    maxwords = 250;} else {    maxwords = 300;}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...