[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Accessing Multiple Input Array

Posted on 2007-11-20
2
Medium Priority
?
605 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

650 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