[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Passing parameters to access array: Problem is parameter acts as a string

Posted on 2005-05-12
5
Medium Priority
?
305 Views
Last Modified: 2010-04-06
I'm trying to develop a page that has dynamically made javascript arrays (written in VBScript with database values embedded in the javascript - the arrays are named arr + the item number).  I have a dropdown list that is dynamically written - names and values are the same as the array names without the arr: just the item number.  If you select an option on the select list, it has an onChange function that passes the selectedIndex.  I am trying to access the arrays through the passed parameter values.

The problem is that the newly constructed array name (that is the same as the array name) is a string and is not treated as an array, therefore, how do I access the arrays in this manner?

Code snippets:
=========
Arrays (after VB compiler make the javascript):
==============================
var arr1 = new Array("0608751","1","2","Cam,Door,Access","Rf-12XX-Ag");
.
.
.
var arr195 = new Array("H0001518","195","2","Cover, Pin-Hammer (M.S.)","**Not Available");


Javascript (also made by the VB compiler):
===========================
function fillItemInformation(formName, itemIndex) {
      arrayName = new array "arr" + formName.options[itemIndex].value;
      alert(arrayName[4]); // this line to test data
}

Javascript call embedded in body of html:
===========================
select name="itemNum" class="dropdownmenu" id="itemNum" onChange="fillItemInformation(this.form.itemNum, this.selectedIndex);">



Thanks!
0
Comment
Question by:drdarby
  • 2
  • 2
5 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 13991859
Hi drdarby,

Perhaps you can try:

eval('var arr1 = new Array("0608751","1","2","Cam,Door,Access","Rf-12XX-Ag");');

Regards,
Zyloch
0
 
LVL 36

Accepted Solution

by:
Zyloch earned 2000 total points
ID: 13991864
Although that may not work since this isn't written in Javascript. Hmm... if you pass that VB string to the Javascript eval function, it should work.

Regards...
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 13992084
I think the easiest thing is for your VB to create an array of arrays:

Arrays (after VB compiler make the javascript):
==============================
var myArrays = new Array();
myArrays[0] = new Array("0608751","1","2","Cam,Door,Access","Rf-12XX-Ag");
.
.
.
myArrays[194] = new Array("H0001518","195","2","Cover, Pin-Hammer (M.S.)","**Not Available");

Now if you have the index, you can access the array using: myArrays[myIndex]


Another solution is to use the "eval" method (this is different from Zyloch's suggestion).
var myArray = eval('arr' + myIndex);
0
 

Author Comment

by:drdarby
ID: 13996175
Hey guys, thanks for all of your help!!  I tryed the first solution presented first and it worked... I did have to add a little to it, but not much!

I had to put an eval statement in the javascript arrays (being written by vbscript) and in the javascript function to handle the arrays. ::

=================================================================
eval('var arr1 = new Array("0608751","1","2","Cam,Door,Access","Rf-12XX-Ag");');
.
.
.
eval('var arr195 = new Array("H0001518","195","2","Cover, Pin-Hammer (M.S.)","**Not Available");');

=================================================================
function fillItemInformation(formName, itemIndex) {
      var thisArray = (eval("arr" + formName.options[itemIndex].value));
      (alert(thisArray[4]));
}

=================================================================
Thanks!
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 13996237
Ok, i probably missunderstood the question.  :°)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

867 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