javascript with input arrays...

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
J_Iceman05
Forum Commoner
Posts: 72
Joined: Wed Aug 03, 2005 10:52 am
Location: Las Vegas, NV

javascript with input arrays...

Post by J_Iceman05 »

I have some code where I am creating an array of textareas based a number of items. So there could be anywhere from 1 and up...
I also want to limit the amount of text that can be entered into the text areas.
I found some code on http://www.mediacollege.com/internet/ja ... cters.html to do that...
but it doesn't seem to be working with the array of textareas.

Right now for testing, i am having it just use 1 textarea. So when i change the name so that it does a "name_1" instead of "name[1]" the javascript works just fine... Thing is... there are so many places where i am alreadying accessing the textareas as arrays, and I just don't want to change it all.

If there is a way to allow me to use the variabled textareas, I will greatly appreciate all the help i can get.
But if that isn't possible... I thank you all anyway.

Thanks in advanced.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

try just passing 'this' as your argument to your function.
User avatar
J_Iceman05
Forum Commoner
Posts: 72
Joined: Wed Aug 03, 2005 10:52 am
Location: Las Vegas, NV

Post by J_Iceman05 »

Javascript function coding

Code: Select all

<script language="javascript" type="text/javascript">
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}
</script>
Textarea (and counter) coding

Code: Select all

<textarea name="limitedtextarea[1]" onKeyDown="limitText(this.form.limitedtextarea,this.form.countdown,100);" 
onKeyUp="limitText(this.form.limitedtextarea,this.form.countdown,100);">
</textarea>
You have <input readonly type="text" name="countdown[1]" size="3" value="100"> characters left.</font>
So, i have the textarea array (where i am limiting the number of characters)
and the input box array (where i am displaying the number left)

... I can put "this" in reference to the text area... but what should i do about the input box that says how many chars are sill available?
(as it won't work if either are arrays)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

try something like this:

untested:

Code: Select all

<script language="javascript" type="text/javascript"> 
limitNum = 15;
function limitText(limitField, limitCnt) 
{ 
   limitCount = document.getElementById(limitCnt);
   if (limitField.value.length > limitNum) 
   { 
      limitField.value = limitField.value.substring(0, limitNum); 
   } 
   else 
   { 
      limitCount.value = limitNum - limitField.value.length; 
   } 
} 
</script>

<body>
<input type="text" name="Leftsample[1]" id="leftsample[1]" value="15"><br>
<textarea name="sample[1]" onKeyDown="limitText(this,'Left'+this.name)"></textarea>
</body>
User avatar
J_Iceman05
Forum Commoner
Posts: 72
Joined: Wed Aug 03, 2005 10:52 am
Location: Las Vegas, NV

Post by J_Iceman05 »

That was a really good idea Burrito. very small amount of tweaking and it worked very well.

Thank you very much.
Post Reply