?
Solved

Accessing Multiple Input Array

Posted on 2007-11-20
2
Medium Priority
?
604 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Suggested Courses

770 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