469,944 Members | 2,344 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,944 developers. It's quick & easy.

How can I limit number of characters in textarea outside of form tag?

Haitashi
This is in the head:
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript">
  2. function limitText(limitField, limitCount, limitNum) {
  3.     if (limitField.value.length > limitNum) {
  4.         limitField.value = limitField.value.substring(0, limitNum);
  5.     } else {
  6.         limitCount.value = limitNum - limitField.value.length;
  7.     }
  8. }
  9. </script>
This is the form:
Expand|Select|Wrap|Line Numbers
  1. <form name="myform">
  2. <textarea name="limitedtextarea" onKeyDown="limitText(this.form.limitedtextarea,this.form.countdown,100);" 
  3. onKeyUp="limitText(this.form.limitedtextarea,this.form.countdown,100);">
  4. </textarea><br>
  5. <font size="1">(Maximum characters: 100)<br>
  6. You have <input readonly type="text" name="countdown" size="3" value="100"> characters left.</font>
  7. </form>
The issue is that the textarea where I plan to use this script doesn't live inside a form tag. And, as you can see, one of the arguments being passed in is the "this.form".

I need the form info for the ELSE of the function. Any suggestions? Adding a form tag is not recommended for me at the moment.

THANKS!
Apr 3 '08 #1
7 1843
hsriat
1,654 Expert 1GB
Instead of making the referance as this.form.limitedtextarea, you can just write this, because you are inside that textarea tag (limitedtextarea).

And for the countdown text, you can use document.getElementById('countdown') (for this you need to add id="countdown" to the countdown input tag)


Regards,
Harpreet
Apr 3 '08 #2
Murdz
34
A textarea should always be inside a <form> tag.
Apr 4 '08 #3
hsriat
1,654 Expert 1GB
A textarea should always be inside a <form> tag.
Not a mandatory requirement!
Apr 4 '08 #4
Yeah, I knew it wasn't mandatory. In the way our application is created, there are specific reasons why they placed this field outside a form tag.

Anyways. I made the suggested changes. My field looks like this:
Expand|Select|Wrap|Line Numbers
  1.     <textarea name="creditNotes" onKeyDown="limitText(this.creditNotes,document.getElementById('countdown'),100);" onKeyUp="limitText(this.creditNotes,document.getElementById('countdown'),100);" id="creditNotes" cols="40" rows="6" style="width:324px; height:82px;"></textarea></div>
  2.             <font size="1">(Maximum characters: 100)<br>
  3.             You have <input readonly type="text" id="countdown" name="countdown" size="3" value="100"> characters left.</font>
The script looks the same as before. However, I an error that states that limitField has no properties. "limitField" should contain the value of the "creditNotes" field.
Apr 4 '08 #5
hsriat
1,654 Expert 1GB
Do this change:
Expand|Select|Wrap|Line Numbers
  1. <textarea name="creditNotes" onKeyDown="limitText(this, document.getElementById('countdown'), 100);" onKeyUp="limitText(this, document.getElementById('countdown'), 100);" id="creditNotes" cols="40" rows="6" style="width:324px; height:82px;"></textarea>
  2.  
  3. </div>
  4.  
  5. <font size="1">(Maximum characters: 100)<br>
  6. You have <input readonly type="text" id="countdown" name="countdown" size="3" value="100"> characters left.</font>
Apr 4 '08 #6
It worked! Thank you very very much Harpreet! ^_^
Apr 4 '08 #7
hsriat
1,654 Expert 1GB
You are welcome. :)

You know when you write this inside a tag, you refer to that particular tag. So there was no need of this.creditNotes. That would return nothing.


Regards,
Harpreet
Apr 4 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Jay | last post: by
4 posts views Thread by wing | last post: by
5 posts views Thread by yawnmoth | last post: by
5 posts views Thread by Jesper Rønn-Jensen | last post: by
6 posts views Thread by tony wong | last post: by
5 posts views Thread by JohnSouth | last post: by
6 posts views Thread by @sh | last post: by
3 posts views Thread by teser3 | last post: by
7 posts views Thread by =?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.