Solved

Accessing Multiple Input Array

Posted on 2007-11-20
2
598 Views
Last Modified: 2008-03-04
I have a JSP generated web page which can contain multiple instances of the following (yes, multiple <select> tags with the same multipleApprovedLoanStatus name):

<select name="multipleApprovedLoanStatus">
  <option value="selected" selected>Please Select</option>
  <option value="Rejected">Reject</option>
  <option value="Approved">Approve</option>
</select>

To access the first instance of multipleApprovedLoanStatus, I can do something like this:

if (document.CreditCardCreditMemoForm.multipleApprovedLoanStatus[0].value == 'selected') {
  ...
}

Notice the hardcoded 0 to access the first instance. However, I do not know how many instances of multipleApprovedLoanStatus there are in the page, so I would need to use a variable:

for (int i = 0; i < size_of_array; i++) {
  if (document.CreditCardCreditMemoForm.multipleApprovedLoanStatus[i].value == 'selected') {
    ...
  }
}

It seems that multipleApprovedLoanStatus[i] is invalid, but multipleApprovedLoanStatus[0] is ok. How do I use the variable correctly to access the array?

Second question: in the for loop above, how do I get the size of the array? I tried document.CreditCardCreditMemoForm.multipleApprovedLoanStatus.length but it is invalid.
0
Comment
Question by:yongsing
2 Comments
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 50 total points
ID: 20326092
When you have multiple fields with the same name, they are accessed as an array in Javascript:
var dropdowns = document.CreditCardCreditMemoForm.multipleApprovedLoanStatus;
for (var i = 0; i < dropdown.length; i++) {
   if (dropdowns[i].options[dropdowns[i].selectedIndex].value == 'selected') {
      ...
   }
}

Open in new window

0
 
LVL 3

Expert Comment

by:aidinet
ID: 20326102
use document.getElementsByName
var arr = document.getElementsByName("multipleApprovedLoanStatus");
for(i=0;i<arr.length;i++){
// do any think on arr[i]
}

Open in new window

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question