SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Problem with Javascript to count chars and prevent additional chars after limit reach

    Hello,

    I have a Javascript which is to count the number of characters entered into a Textarea box and
    to prevent any more characters from being added after a certain number has been reached.

    Here is the code:

    Code JavaScript:
    function textCounter(field,cntfield,maxlimit) {
    	if (field.value.length > maxlimit) {
    		field.value = field.value.substring(0, maxlimit);}
    	else 
    		{cntfield.value = maxlimit - field.value.length;}
    }


    And it is called from inside of a form like this:


    Code HTML4Strict:
     <tr bgcolor="#e4e8f0"> 
                            <td colspan="3">&nbsp;</td>
                          </tr>
                          <tr bgcolor="#c6cedf"> 
                            <td colspan="3"> Please enter any suggestion<br>
                              <div align="center"> 
                                <textarea name="comments"  cols="40" rows="3" wrap="VIRTUAL" 
    				  onKeyDown="textCounter(document.frm.comments,document.frm.remLen1,150)"
    				    onKeyUp="textCounter(document.frm.comments,document.frm.remLen1,150)"></textarea>
                          <br>
                          <input readonly type="text" name="remLen1" size="3" maxlength="3" value="150">
                          characters left <br>
                              </div>
                            </td>
                          </tr>

    What am I doing wrong?
    Appreciate your replies.

    Best regards,

    P.S., The amazing thing is that the above code is working in some pages, but not all!!

    Anoox search engine volunteer

    www.anoox.com

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,250
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)

    byId

    I think it may work better if you use the document.getElementById('id') syntax and pass the id value as an argument. Also, do you need both onKeyDown and onKeyUp ??

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had done the same before like this:
    Code javascript:
    function countChar(container, numChars, chars) {
    	if (container.value.length > chars){
    		container.value = container.value.substring(0, chars);
    	}
    	numChars.value = container.value.length;
    }
    Code HTML4Strict:
    <form name="myForm1" id="myForm1" method="post" action="">
    	<textarea id="myDesc" name="myDesc" rows="5" cols="40" onKeyUp="countChar(this, this.form.charNum, 10);"></textarea><br />
    	<input type="text" id="charNum" name="charNum" value="0" /><br>
    	<input type="submit" id="submit" name="submit" value="Submit" />
    </form>
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your suggestion.
    It works Ok now after I changed to document.getElementById('id')

    Best regards


    Quote Originally Posted by Mittineague View Post
    I think it may work better if you use the document.getElementById('id') syntax and pass the id value as an argument. Also, do you need both onKeyDown and onKeyUp ??

    Anoox search engine volunteer

    www.anoox.com


Bookmarks

Posting Permissions

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