Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-03-2012, 01:43 AM   PM User | #1
arfa
New Coder

 
Join Date: Sep 2006
Posts: 51
Thanks: 1
Thanked 0 Times in 0 Posts
arfa is an unknown quantity at this point
limit character input (by count) with : in textarea name

I have a nice js character limit script I have used for a long time with no problem.
Code:
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else { limitCount.value = limitNum - limitField.value.length; }}
I now have a form processor that has a naming system
Code:
textarea name=5)MIN:TXT:message
the element trigger looks like
Code:
<textarea name=5)MIN:TXT:message cols=40 rows=14 
onKeyDown=\"limitText(this.form.5)MIN:TXT:message,this.form.countdown,10);\"
onKeyUp=\"limitText(this.form.5)MIN:TXT:message,this.form.countdown,10);\">$value[5]</textarea>
And the limit script baulks at the characters ) : in the name.

I have tried quotes, escaped quotes and characters and various incantations but can't seem to convince it to work.

Any suggested fix on this one - or pointers to another similar script that will cope with this naming system.

thanks
arfa is offline   Reply With Quote
Old 10-03-2012, 02:00 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Actually, it balks even at the leading digit in the name.

If you won't follow normal naming rules for form fields you have to
(1) Ensure that you put "..." around the entire name.
(2) Use form["..."] in the JS code to refer to the field.

BUT...

But you really never needed to refer to THAT <textarea> by name in that JS code, in the first place.

Just do this:
Code:
<textarea name="5)MIN:TXT:message"
         onkeydown="limitText(this,this.form.countdown,10);"
         onkeyup  ="limitText(this,this.form.countdown,10);"
><?php echo $value[5];?></textarea>
Notice carefully the quotes around the field name.
Notice that just this alone specifies the current field! nothing else needed.

Stop using PHP to output complex HTML. Do as I show there: Output the HTML, yes, and then only use the minimal PHP needed to output the contents.

Oh...and upper case letters in event handler names is obsolete. onkeyup and onkeydown are correct.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 10-03-2012, 02:20 AM   PM User | #3
arfa
New Coder

 
Join Date: Sep 2006
Posts: 51
Thanks: 1
Thanked 0 Times in 0 Posts
arfa is an unknown quantity at this point
Yes - and yes again.

The naming system fits with a generic form processor with lots of options - fed via the name.

And - this,this.form.
part of (this) element - obvious now you point it out.

>> Stop using PHP to output complex HTML
Yes, I do tend to just set up <? and type away.

Thanks so much.
You have helped me before -
arfa is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:57 PM.


Advertisement
Log in to turn off these ads.