Solved

Accessing Multiple Input Array

Posted on 2007-11-20
2
596 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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now