Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2004
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Add the characters in the fields

    Here is the character count script I am working with:
    http://codeline-alley.digiserv.net/j...cter_count.php

    I don't know how to use it so that the character count for multiple fields (text areas) is totaled and displayed in the "characters left" field.

    Any help would be greatly appreciated.

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is this the page you are applying the code too?

    If so, I just checked at it appears to be working.
    At least for two fields anyway....

    .....Willy

  • #3
    New Coder
    Join Date
    Jun 2004
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply. I found cross browser compatible code on this forum so I thought I would repost. That page you linked to is a page that does what I want to do, but it is not my page. I'm where I was before - minus the cross browser issue.

  • #4
    Regular Coder
    Join Date
    Jun 2004
    Location
    underground
    Posts
    186
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, I don't know what's going on with the replies above, but in response to the original question, this might work:
    Code:
    <body>
    <script type="text/javascript">
    function doCount(elem, count, limit, names)
     {
       names = names.split("+");
       var pForm = elem.form,
           total = 0,
             val = elem.value,
              ln = names.length,
               i = -1; 
       while(++i < ln)
         total += pForm[names[i]].value.length;
       total > limit ? elem.value = val.substr(0, val.length - 1) : count.value = limit - total;
     }
    </script>
    <form>
    <fieldset style="text-align:center;padding-bottom:1em">
    <legend>
    <label>characters remaining: </label>
    <input name="counter" value="20" size="4" readonly>
    </legend>
    <textarea name="txt1" cols="20" rows="2" 
    onkeydown="doCount(this, counter, 20, 'txt1+txt2+txt3')" onkeyup="doCount(this, counter, 20, 'txt1+txt2+txt3')"></textarea>
    <textarea name="txt2" cols="20" rows="2" 
    onkeydown="doCount(this, counter, 20, 'txt1+txt2+txt3')" onkeyup="doCount(this, counter, 20, 'txt1+txt2+txt3')"></textarea>
    <textarea name="txt3" cols="20" rows="2" 
    onkeydown="doCount(this, counter, 20, 'txt1+txt2+txt3')" onkeyup="doCount(this, counter, 20, 'txt1+txt2+txt3')"></textarea>
    </fieldset>
    </form>
    </body>
    Edit:
    I hadn't tested that in Opera: a browser that requires an additional onkeypress handler...
    it also added my linebreaks to the textarea values (those have been removed).

    I suppose all of the parameters should be put into the function as variables, given the number of event handlers that they'd be stuffed into.
    Last edited by neofibril; 06-26-2004 at 08:33 PM.

  • #5
    New Coder
    Join Date
    Jun 2004
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Sorry for the delayed reply. It works perfectly! Thank you very much neofibril and Willy Duitt for all your help. It is greatly appreciated.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •