Count words in multiple forms

Discussion in 'JavaScript' started by glasglow, Jun 24, 2007.

  1. #1
    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?
     
    glasglow, Jun 24, 2007 IP
  2. NoamBarz

    NoamBarz Active Member

    Messages:
    242
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Please explain a little more. I don't really understand what you want...
     
    NoamBarz, Jun 25, 2007 IP
  3. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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
     
    UnrealEd, Jun 25, 2007 IP
  4. glasglow

    glasglow Active Member

    Messages:
    926
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #4
    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>
     
    glasglow, Jun 25, 2007 IP
  5. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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):
     
    UnrealEd, Jun 26, 2007 IP
  6. glasglow

    glasglow Active Member

    Messages:
    926
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #6
    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>
     
    glasglow, Jun 26, 2007 IP
  7. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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):
     
    UnrealEd, Jun 28, 2007 IP
  8. glasglow

    glasglow Active Member

    Messages:
    926
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #8
    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.
     
    glasglow, Jun 29, 2007 IP
  9. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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.
     
    UnrealEd, Jun 29, 2007 IP
  10. glasglow

    glasglow Active Member

    Messages:
    926
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #10
    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.
     
    glasglow, Jun 29, 2007 IP
  11. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #11
    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.
     
    UnrealEd, Jun 29, 2007 IP
  12. glasglow

    glasglow Active Member

    Messages:
    926
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #12
    Thank you very much! You are sensai!
     
    glasglow, Jun 29, 2007 IP
  13. apalm

    apalm Guest

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    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
     
    apalm, Jul 9, 2007 IP