Right now I am counting words in an input box ... function cnt(w,x){ var y=w.value; var r = 0; a=y.replace(/\s/g,' '); a=a.split(' '); for (z=0; z<a.length; z++) {if (a[z].length > 0) r++;} x.value=r; } and calling it from the form: onkeyup="cnt(this,document.myform.c)" My problem is the "onkeyup"... I want to be able to count two forms. But since it's keyup it sets it back to 0 when I change forms. Anyway to add the totals of both forms and keep an ongoing count in stead of 1 or the other?
instead of storing the count value in a (hidden) field, named c, you can store it in a variable in javascript: var c = 0; function cnt(w){ var y=w.value; var r = 0; a=y.replace(/\s/g,' '); a=a.split(' '); for (z=0; z<a.length; z++) {if (a[z].length > 0) r++;} c = r; } Code (markup): the variable is accessible for both forms, so the count won't be reset
It seems logical.. but how do I call it? Here is my original form: <form name="myform"> <textarea rows="15" name="w" cols="45" onkeyup="cnt(this,document.myform.c)"></textarea> <br />Word Count: <input type="text" name="c" value="0" size="5" onkeyup="cnt(document.myform.w,this)" /> </form>
same as you do atm, but you don't enter the second parameter. So instead of: cnt(this,document.myform.c) Code (markup): you use: cnt(this) Code (markup):
I'm not getting reponse from it yet this way. I have: <html> <head> <script> var c = 0; function cnt(w){ var y=w.value; var r = 0; a=y.replace(/\s/g,' '); a=a.split(' '); for (z=0; z<a.length; z++) {if (a[z].length > 0) r++;} c = r; } </script> </head> <body> <form name="myform"> <textarea rows="15" name="w" cols="45" onkeyup="cnt(this)"></textarea> <br />Word Count: <input type="text" name="c" value="0" size="5" onkeyup="cnt(document.myform.w,this)"/> </form> </body> </html>
you have to change this one as well: cnt(document.myform.w,this) Code (markup): All occurences of the cnt() function should be replaced with this: cnt(this) Code (markup):
Have you used this code? I have tried and tried and tried and tried to use this as you have said and once I make the changes I am getting a script error.
i just did What exactly do you want? i read your first post again, and it says something about 2 forms and a field in each form. I only see 1 form, and 1 field? Do you want to display the value of the variable c in the "c" input field? or do you want to count the words in the "c" textfield as well? If you just want to know the number of words entered in the textarea, i think it's best to check it when the form is submitted, not whenever a user changes some text in the textarea.
Yes the first example was 1 text area because that what I was using that worked. I'd like two text areas if possible though. With the same setup so it counts live while I type. Before when I tried to add two text areas it would return to 0 if I changed from one text area to the next.
i think the only way to do this, is by using arrays. You can't simply add the current words to the previous calculated words, because you will keep adding the number of words, instead of updating (can't find the right words to explain, i'll give it another try if you don't understand). this will probably work: var c = new Array(); c[0] = 0; c[1] = 0; var total = 0; function cnt(w, field){ var y=w.value; var r = 0; a=y.replace(/\s/g,' '); a=a.split(' '); for (z=0; z<a.length; z++) {if (a[z].length > 0) r++;} c[field] = r; calculateTotal(); } function calculateTotal(){ for ( var i=0; i<c.length; i++ ) { total += c[i]; } } Code (markup): and when you call it, you use the following code: cnt(this, 0) Code (markup): for the first field, and cnt(this, 1) for the second field. You can add some more fields if you want, just by adding another c = 0; after the allready existing ones.
Awesome that I found a thread about exactly what I need. I don't fully understand how to make it happen though, I tried the code above but doesn't get the value of the two fields' added counts to show. Can you please write out the entire code for two fields and one field showing the count? Thank you so much if you do this. / Palm