• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 310
  • Last Modified:

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

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
drdarby
Asked:
drdarby
  • 2
  • 2
1 Solution
 
ZylochCommented:
Hi drdarby,

Perhaps you can try:

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

Regards,
Zyloch
0
 
ZylochCommented:
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
 
GrandSchtroumpfCommented:
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
 
drdarbyAuthor Commented:
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
 
GrandSchtroumpfCommented:
Ok, i probably missunderstood the question.  :°)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now