Solved

Accessing Multiple Input Array

Posted on 2007-11-20
2
600 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

821 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