Changes to Count Function

Discussion in 'JavaScript' started by adamjblakey, Nov 6, 2009.

  1. #1
    Hi,

    I have this function:

    
    function limitText(limitField, limitCount, limitNum) {
    	if (limitField.value.length > limitNum) {
    		limitField.value = limitField.value.substring(0, limitNum);
    	} else {
    		limitCount.value = limitNum - limitField.value.length;
    	}
    }
    
    Code (markup):
    
    <textarea name="desc" onKeyDown="limitText(this.form.desc,this.form.countdown,500);" 
    onKeyUp="limitText(this.form.desc,this.form.countdown,500);">Testing</textarea><br>
    You have <input readonly type="text" name="countdown" size="3" value="500"> characters left.
    
    Code (markup):
    The only problem with it though is if there is already text within the field it will not count this until you start typing. Is there any way to count the text that is already in there.

    Cheers,
    Adam
     
    adamjblakey, Nov 6, 2009 IP
  2. rajesharma470

    rajesharma470 Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think you are facing this problem only when you come to the page with text already in textarea(name="desc").
    Simple solution is call the 'limitText' function 'onload' of the page 'body' tag.
     
    rajesharma470, Nov 10, 2009 IP
  3. ajaXpert

    ajaXpert Member

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    you jjust need to run limitText function after the textarea code once.
     
    ajaXpert, Dec 4, 2009 IP