JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 24th, 2012, 10:30 AM
nikko50 nikko50 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 480 nikko50 User rank is Corporal (100 - 500 Reputation Level)nikko50 User rank is Corporal (100 - 500 Reputation Level)nikko50 User rank is Corporal (100 - 500 Reputation Level)nikko50 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 14 h 11 m 30 sec
Reputation Power: 11
Form field array??

Hi all!

I have a several duplicate form field names like below "car[]". Let's say if I want to input the value "Hello" in the one of them how can I do this? I will be using a javascript function where I will pass a number such as "2". So I would like the function to input the vaule "hello" in the 2nd instance of the "car[]" variable in my form "form". How can I do this?


<form name="form" method="post">
<table > <tr><td> CAR #<br><input type='text' class='field' name='car[]' size='1'></td></tr></table>
<table > <tr><td> CAR #<br><input type='text' class='field' name='car[]' size='1'></td></tr></table>
<table > <tr><td> CAR #<br><input type='text' class='field' name='car[]' size='1'></td></tr></table>
<table > <tr><td> CAR #<br><input type='text' class='field' name='car[]' size='1'></td></tr></table>
</form>

Reply With Quote
  #2  
Old September 24th, 2012, 11:21 AM
web_loone08's Avatar
web_loone08 web_loone08 is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2008
Posts: 662 web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 9 h 15 m 9 sec
Reputation Power: 69
Code:
<script>
function insertText(text2insert,key)
{
document.getElementsByTagName("form")[0].getElementsByTagName("input")[key-1].value = text2insert;
}
</script>

<form name="form" method="post">
<table > <tr><td> CAR #<br><input type='text' class='field' name='car[]' size='1'></td></tr></table>
<table > <tr><td> CAR #<br><input type='text' class='field' name='car[]' size='1'></td></tr></table>
<table > <tr><td> CAR #<br><input type='text' class='field' name='car[]' size='1'></td></tr></table>
<table > <tr><td> CAR #<br><input type='text' class='field' name='car[]' size='1'></td></tr></table>
<input type="button" value="Insert Text" onclick="insertText('Hello','2')"/>
</form>

Reply With Quote
  #3  
Old September 24th, 2012, 12:10 PM
nikko50 nikko50 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 480 nikko50 User rank is Corporal (100 - 500 Reputation Level)nikko50 User rank is Corporal (100 - 500 Reputation Level)nikko50 User rank is Corporal (100 - 500 Reputation Level)nikko50 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 14 h 11 m 30 sec
Reputation Power: 11
Hi,
There are other form fields inbetween the "car[]" fields. I forgot to mention that. I just stripped down my code so it would be easier to read. I really need the function to only look at the "car[]" name fields and find the instance. Can you help?

Reply With Quote
  #4  
Old September 24th, 2012, 12:29 PM
web_loone08's Avatar
web_loone08 web_loone08 is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2008
Posts: 662 web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 9 h 15 m 9 sec
Reputation Power: 69
You can just adapt it a little bit then:

Code:
<script>
function insertText(text2insert,formKey,inputKey)
{
document.getElementsByTagName("form")[formKey-1].getElementsByTagName("input")[inputKey-1].value = text2insert;
}
</script>

<form>
<input type="text"/>
</form>

<form name="form" method="post">
<table > <tr><td> CAR #<br><input type='text' class='field' name='car[]' size='1'></td></tr></table>
<table > <tr><td> CAR #<br><input type='text' class='field' name='car[]' size='1'></td></tr></table>
<table > <tr><td> CAR #<br><input type='text' class='field' name='car[]' size='1'></td></tr></table>
<table > <tr><td> CAR #<br><input type='text' class='field' name='car[]' size='1'></td></tr></table>
<input type="button" value="Insert Text" onclick="insertText('Hello','2','2')"/>
</form>

<form>
<input type="text"/>
</form>


Where the "formKey" parameter is a number representation of the form location; in relation to the element's page position (from top to bottom).
Comments on this post
nikko50 agrees!

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Form field array??

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap