473,382 Members | 1,329 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

trouble accessing form array values

Samishii23
246 100+
So I have a dynamically created form.
This is a quick run down of what happens.

Page Loads -> jQuery Ajax load page -> Another Ajax loads form:
Expand|Select|Wrap|Line Numbers
  1. <form id="listForm" name="listForm">
  2. <input type="text" name="listQty[]" value="0">
  3. <input type="checkbox" name="listCb[]" value="php id #">
  4. <input type="hidden" name="listTitle[]" value="BlahBlah">
  5. (Up to 7 more sets of items currently)
  6. </form>
I found this method:
Expand|Select|Wrap|Line Numbers
  1. var arrayElements = document.listForm.elements["listQty[]"];
When I try to loop through the values of each element, it just gives me 0 everytime even if I put something else into the text box.

What am I ( and that website... ) doing wrong?
Jan 5 '11 #1

✓ answered by Dormilich

true, but your loop is wrong. for looping through a list you use a standard for() loop, not the for … in loop. further, i refers to the key, which is either a string or a number.
Expand|Select|Wrap|Line Numbers
  1. var ele, eleList = document.formList.elements["listQty[]"];
  2. for (var i = 0, l = eleList.length; i < l; i += 1) {
  3.     ele = eleList[i];
  4.     // …
  5. }

9 1591
Brian Connelly
103 100+
Not sure about this...Im learning. But if you remove the value 0 from the array. Does it work? I believe that you assigned the array a 0 memory location, but if you remove that value, each time you enter a value in the form it will append an new index.
Jan 5 '11 #2
Rabbit
12,516 Expert Mod 8TB
I'm not sure what you're trying to do but if you're trying to do but if you're trying to return the array of form elements, then you need to stop at .elements. The rest of it will return only one element.

If you are trying to return the value of the element, then you need .value.
Jan 5 '11 #3
Samishii23
246 100+
Sorry, I'll clarify a little bit. This is what I'm doing:
Expand|Select|Wrap|Line Numbers
  1. var eleArray = document.formList.elements["listQty[]"];
  2.  
  3. for ( var i in eleArray ) {
  4.   alert(i.value); // undefined
  5.   alert(i); // 0
  6.   }
Jan 5 '11 #4
Rabbit
12,516 Expert Mod 8TB
Well, listQty[] isn't an array. It's the name of a textbox so it has no elements. Just use document.formList.elements.
Jan 5 '11 #5
Samishii23
246 100+
Going by my last post, am I not?
Jan 5 '11 #6
Rabbit
12,516 Expert Mod 8TB
No, you're not. You're using document.formList.elements["listQty[]"], which will return the textbox, which has no elements. You need to use document.formList.elements, which will return the array of elements in formList.
Jan 5 '11 #7
Samishii23
246 100+
I would say that would be fine if only "listQty[]" was in the form, but I will have 3 others like the previously metioned. Doesn't form.elements[] if given a string return all the elements matching?
Jan 5 '11 #8
Dormilich
8,658 Expert Mod 8TB
true, but your loop is wrong. for looping through a list you use a standard for() loop, not the for … in loop. further, i refers to the key, which is either a string or a number.
Expand|Select|Wrap|Line Numbers
  1. var ele, eleList = document.formList.elements["listQty[]"];
  2. for (var i = 0, l = eleList.length; i < l; i += 1) {
  3.     ele = eleList[i];
  4.     // …
  5. }
Jan 5 '11 #9
Rabbit
12,516 Expert Mod 8TB
I see, I thought you were trying to return all the form elements but you were trying to return all form elements with the same name.
Jan 5 '11 #10

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

Similar topics

2
by: rhungund | last post by:
Hey all... I'm submitting various form variables as arrays, so when I do try to access them when the form is submitted, i do a split on the variable and pop it into an array. for example:...
6
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
10
by: Geoff Cox | last post by:
Hello, I have a series of pages each of which creates an array of values. How do I keep all the array values until the last page? Cheers Geoff
3
by: Olivier BESSON | last post by:
Hello, I have a web service of my own on a server (vb.net). I must declare it with SoapRpcMethod to be used with JAVA. This is a simple exemple method of my vb source : ...
0
by: aakash | last post by:
Hello Guys I am upsizing ms access project to give it a ms sql connectivity I am having problem in accessing form control values in ms sql function CREATE FUNCTION "ReportList DateRange"() ...
3
by: judy.j.miller | last post by:
Does anyone know why i can't access a form element value using dot notation in firefox, when i'm in a function. Works ok in the body. I'm trying to do this: var FarTemp = faren.temp.value; I...
1
by: DavidB | last post by:
I am working with a database and I would like to be able to populate an array at run time and then use the values that were pushed into the array as criteria for a select query. It seems that this...
2
by: 4Ankit | last post by:
hey guys i am having trouble changing my array code to include a 'for/ in' structure the code i am trying to change is below: <script type="text/javascript"> var contents = new Array(3)
9
by: Nathan Sokalski | last post by:
I am trying to use the System.Array.ForEach method in VB.NET. The action that I want to perform on each of the Array values is: Private Function AddQuotes(ByVal value As String) As String Return...
5
by: Geethu03 | last post by:
Hi i have a array values in hidden format <form name="form2" method="post" action="test.php"> <input type="hidden" name="date_val" value="<%= date %>"> <input type="hidden" name="name_val"...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.