0

I am trying to have to have a form with two drop down select options that are populated from arrays. The first select has only one array, however depending on the selection, I want to populate the second with a specific multidimensional array. I am able to do them individually, but I am unable to pass the array name from the first selection to the function to populate the second.

I am pretty new to javascript and jQuery 1.11.0 and would appreciate any assistance.

Here is the code I am working with.

select name="aircraftList" id="aircraftList" onchange="showAircraft(this.selectedIndex)"

select name="configWBList" id="configWBList"

// values for AIRCRAFT
// tnumber, acmodel, maxwt, f1arm, m1arm, r1arm, littarm, bag1max, fuel1kgmax, 
/////////////////////////////////////////////////////////// 
var aircraft = [
    ["Select Aircraft", "", "", "", "", "", "", "", "", ""],
    ['N901AH', "Agusta A109E", 3000, 1585, 2455, 3200, 5300, 3200, 150, 480],
    ['N902AH', "Agusta A109E", 3000, 1585, 2455, 3200, 5300, 3200, 150, 480],
    ['N903AH', "Agusta A109E", 3000, 1585, 2455, 3200, 5300, 3200, 150, 480],
    ['N904AH', "Agusta A109E", 3000, 1585, 2455, 3200, 5300, 3200, 150, 480],
    ['N905AH', "Agusta A109E", 3000, 1585, 2455, 3200, 5300, 3200, 150, 480],
    ['N907AH', "Agusta A109E", 3000, 1585, 2455, 3200, 5300, 3200, 150, 480]
];

// values for AC N901AH
/////////////////////////////////////////////////////////// 
var N901AH = [
    ["Select a Configuration", "Select Config", "Select Config"],
    ["6 PAX STANDARD SEATS OFFSHORE", 2205.61, 3485.69],
    ["DUAL CONTROLS INSTALLED", 2207.81, 3483.76],
    ["VIP SEATS INSTALLED", 2212.11, 3483.75],
    ["VIP SEATS, DUAL CONTROLS INSTALLED", 2214.31, 3481.83],
    ["ONE LITTER KIT INSTALLED", 2213.91, 3483.50],
    ["ONE LITTER, DUAL CONTROLS INSTALLED", 2216.11, 3481.58],
    ["6 PAX STANDARD SEATS, VESTS REMOVED", 2196.81, 3489.83],
    ["VIP SEATS INSTALLED, VEST REMOVED", 2203.31, 3487.87],
    ["ONE LITTER INSTALLED, VESTS REMOVED", 2207.31, 3487.02],
    ["TWO LITTER INSTALLED", 2222.21, 3481.32],
    ["TWO LITTER, DUAL CONTROLS INSTALLED", 2224.41, 3479.41],
    ["VIP SEATS, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2205.51, 3485.93],
    ["ONE LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2209.51, 3485.09],
    ["TWO LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2220.01, 3482.33],
    ["FLOATS, RAFT, VESTS REMOVED", 2084.51, 3507.26]
];

// values for AC N902AH
/////////////////////////////////////////////////////////// 
var N902AH = [
    ["Select a Configuration", "Select Config", "Select Config"],
    ["6 PAX STANDARD SEATS OFFSHORE", 2205.61, 3485.69],
    ["DUAL CONTROLS INSTALLED", 2207.81, 3483.76],
    ["VIP SEATS INSTALLED", 2212.11, 3483.75],
    ["VIP SEATS, DUAL CONTROLS INSTALLED", 2214.31, 3481.83],
    ["ONE LITTER KIT INSTALLED", 2213.91, 3483.50],
    ["ONE LITTER, DUAL CONTROLS INSTALLED", 2216.11, 3481.58],
    ["6 PAX STANDARD SEATS, VESTS REMOVED", 2196.81, 3489.83],
    ["VIP SEATS INSTALLED, VEST REMOVED", 2203.31, 3487.87],
    ["ONE LITTER INSTALLED, VESTS REMOVED", 2207.31, 3487.02],
    ["TWO LITTER INSTALLED", 2222.21, 3481.32],
    ["TWO LITTER, DUAL CONTROLS INSTALLED", 2224.41, 3479.41],
    ["VIP SEATS, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2205.51, 3485.93],
    ["ONE LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2209.51, 3485.09],
    ["TWO LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2220.01, 3482.33],
    ["FLOATS, RAFT, VESTS REMOVED", 2084.51, 3507.26]
];

// values for AC N903AH
/////////////////////////////////////////////////////////// 
var N903AH = [
    ["Select a Configuration", "Select Config", "Select Config"],
    ["6 PAX STANDARD SEATS OFFSHORE", 2205.61, 3485.69],
    ["DUAL CONTROLS INSTALLED", 2207.81, 3483.76],
    ["VIP SEATS INSTALLED", 2212.11, 3483.75],
    ["VIP SEATS, DUAL CONTROLS INSTALLED", 2214.31, 3481.83],
    ["ONE LITTER KIT INSTALLED", 2213.91, 3483.50],
    ["ONE LITTER, DUAL CONTROLS INSTALLED", 2216.11, 3481.58],
    ["6 PAX STANDARD SEATS, VESTS REMOVED", 2196.81, 3489.83],
    ["VIP SEATS INSTALLED, VEST REMOVED", 2203.31, 3487.87],
    ["ONE LITTER INSTALLED, VESTS REMOVED", 2207.31, 3487.02],
    ["TWO LITTER INSTALLED", 2222.21, 3481.32],
    ["TWO LITTER, DUAL CONTROLS INSTALLED", 2224.41, 3479.41],
    ["VIP SEATS, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2205.51, 3485.93],
    ["ONE LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2209.51, 3485.09],
    ["TWO LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2220.01, 3482.33],
    ["FLOATS, RAFT, VESTS REMOVED", 2084.51, 3507.26]
];

// values for AC N904AH
/////////////////////////////////////////////////////////// 
var N904AH = [
    ["Select a Configuration", "Select Config", "Select Config"],
    ["6 PAX STANDARD SEATS OFFSHORE", 2205.61, 3485.69],
    ["DUAL CONTROLS INSTALLED", 2207.81, 3483.76],
    ["VIP SEATS INSTALLED", 2212.11, 3483.75],
    ["VIP SEATS, DUAL CONTROLS INSTALLED", 2214.31, 3481.83],
    ["ONE LITTER KIT INSTALLED", 2213.91, 3483.50],
    ["ONE LITTER, DUAL CONTROLS INSTALLED", 2216.11, 3481.58],
    ["6 PAX STANDARD SEATS, VESTS REMOVED", 2196.81, 3489.83],
    ["VIP SEATS INSTALLED, VEST REMOVED", 2203.31, 3487.87],
    ["ONE LITTER INSTALLED, VESTS REMOVED", 2207.31, 3487.02],
    ["TWO LITTER INSTALLED", 2222.21, 3481.32],
    ["TWO LITTER, DUAL CONTROLS INSTALLED", 2224.41, 3479.41],
    ["VIP SEATS, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2205.51, 3485.93],
    ["ONE LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2209.51, 3485.09],
    ["TWO LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2220.01, 3482.33],
    ["FLOATS, RAFT, VESTS REMOVED", 2084.51, 3507.26]
];

// values for AC N905AH
/////////////////////////////////////////////////////////// 
var N905AH = [
    ["Select a Configuration", "Select Config", "Select Config"],
    ["6 PAX STANDARD SEATS OFFSHORE", 2205.61, 3485.69],
    ["DUAL CONTROLS INSTALLED", 2207.81, 3483.76],
    ["VIP SEATS INSTALLED", 2212.11, 3483.75],
    ["VIP SEATS, DUAL CONTROLS INSTALLED", 2214.31, 3481.83],
    ["ONE LITTER KIT INSTALLED", 2213.91, 3483.50],
    ["ONE LITTER, DUAL CONTROLS INSTALLED", 2216.11, 3481.58],
    ["6 PAX STANDARD SEATS, VESTS REMOVED", 2196.81, 3489.83],
    ["VIP SEATS INSTALLED, VEST REMOVED", 2203.31, 3487.87],
    ["ONE LITTER INSTALLED, VESTS REMOVED", 2207.31, 3487.02],
    ["TWO LITTER INSTALLED", 2222.21, 3481.32],
    ["TWO LITTER, DUAL CONTROLS INSTALLED", 2224.41, 3479.41],
    ["VIP SEATS, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2205.51, 3485.93],
    ["ONE LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2209.51, 3485.09],
    ["TWO LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2220.01, 3482.33],
    ["FLOATS, RAFT, VESTS REMOVED", 2084.51, 3507.26]
];

// values for AC N907AH
/////////////////////////////////////////////////////////// 
var N907AH = [
    ["Select a Configuration", "Select Config", "Select Config"],
    ["6 PAX STANDARD SEATS OFFSHORE", 2205.61, 3485.69],
    ["DUAL CONTROLS INSTALLED", 2207.81, 3483.76],
    ["VIP SEATS INSTALLED", 2212.11, 3483.75],
    ["VIP SEATS, DUAL CONTROLS INSTALLED", 2214.31, 3481.83],
    ["ONE LITTER KIT INSTALLED", 2213.91, 3483.50],
    ["ONE LITTER, DUAL CONTROLS INSTALLED", 2216.11, 3481.58],
    ["6 PAX STANDARD SEATS, VESTS REMOVED", 2196.81, 3489.83],
    ["VIP SEATS INSTALLED, VEST REMOVED", 2203.31, 3487.87],
    ["ONE LITTER INSTALLED, VESTS REMOVED", 2207.31, 3487.02],
    ["TWO LITTER INSTALLED", 2222.21, 3481.32],
    ["TWO LITTER, DUAL CONTROLS INSTALLED", 2224.41, 3479.41],
    ["VIP SEATS, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2205.51, 3485.93],
    ["ONE LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2209.51, 3485.09],
    ["TWO LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2220.01, 3482.33],
    ["FLOATS, RAFT, VESTS REMOVED", 2084.51, 3507.26]
];


// set form values to selected aircraft option  
///////////////////////////////////////////////////////////
function showAircraft(idx) {
    var aircraftWB = [aircraft[idx][0]]
    alert(aircraftWB);
    $("#configWBList").html('');
    list(aircraftWB);
}

function list(arrary) {
    alert(arrary);

    // populate selected aircraft w&b select
    ///////////////////////////////////////////////////////////
    for (i = 0; i < arrary.length; i++) {
        var select = document.getElementById("configWBList");
        select.options[select.options.length] = new Option(arrary[i][0], arrary[i][1]);
    }

    // reset the selected aircraft configuration to standard
    ///////////////////////////////////////////////////////////
    $("#configWBList").val($("#configWBList option:first").val());
    $("#configWBList").selectmenu('refresh', true);

}


// initialise form
// ****************************************************
function initForm() {
    // populate aircraft select
    ///////////////////////////////////////////////////////////
    for (i = 0; i < aircraft.length; i++) {
        var select = document.getElementById("aircraftList");
        select.options[select.options.length] = new Option(aircraft[i][0], aircraft[i][1]);
    }
}

initForm();

https://jsfiddle.net/g3h9p2ur/

2
  • Are you stuck with the information in that form or are you writing those arrays yourself? If you can modify them, associative arrays might be a better choice. Commented Apr 1, 2015 at 4:33
  • No, I am writing those arrays myself. As I said I am very new to jQuery (my first project) and I could not get associative arrays to work. Commented Apr 1, 2015 at 17:06

2 Answers 2

2

What I would suggest is to create an object from the arrays like:

var aircrafts = { "N901AH" : N901AH, "N902AH" : N902AH, "N903AH" : N903AH, "N904AH" : N904AH, "N905AH" : N905AH, "N907AH" : N907AH};

Also instead of for loop use $.each(array, handler(index, element)). Then instead of eval() use the option text to get the array you need from the object:

console.log(aircrafts["N901AH"]);//example

and populate the second select. Here is my fiddle on this: jsfiddle for my answer

Regards.

Sign up to request clarification or add additional context in comments.

2 Comments

is there a reason not to the the eval()?
it converts string into executable JavaScript. it is a possible breaking point for attack. More in the link: jslinterrors.com/eval-is-evil
0

You can use "eval" to get the result variable data.

<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script language="javascript">
var aircraft = [
    ["Select Aircraft", "", "", "", "", "", "", "", "", ""],
    ['N901AH', "Agusta A109E", 3000, 1585, 2455, 3200, 5300, 3200, 150, 480],
    ['N902AH', "Agusta A109E", 3000, 1585, 2455, 3200, 5300, 3200, 150, 480],
    ['N903AH', "Agusta A109E", 3000, 1585, 2455, 3200, 5300, 3200, 150, 480],
    ['N904AH', "Agusta A109E", 3000, 1585, 2455, 3200, 5300, 3200, 150, 480],
    ['N905AH', "Agusta A109E", 3000, 1585, 2455, 3200, 5300, 3200, 150, 480],
    ['N907AH', "Agusta A109E", 3000, 1585, 2455, 3200, 5300, 3200, 150, 480]
];

// values for AC N901AH
/////////////////////////////////////////////////////////// 
var N901AH = [
    ["Select a Configuration1", "Select Config", "Select Config"],
    ["6 PAX STANDARD SEATS OFFSHORE", 2205.61, 3485.69],
    ["DUAL CONTROLS INSTALLED", 2207.81, 3483.76],
    ["VIP SEATS INSTALLED", 2212.11, 3483.75],
    ["VIP SEATS, DUAL CONTROLS INSTALLED", 2214.31, 3481.83],
    ["ONE LITTER KIT INSTALLED", 2213.91, 3483.50],
    ["ONE LITTER, DUAL CONTROLS INSTALLED", 2216.11, 3481.58],
    ["6 PAX STANDARD SEATS, VESTS REMOVED", 2196.81, 3489.83],
    ["VIP SEATS INSTALLED, VEST REMOVED", 2203.31, 3487.87],
    ["ONE LITTER INSTALLED, VESTS REMOVED", 2207.31, 3487.02],
    ["TWO LITTER INSTALLED", 2222.21, 3481.32],
    ["TWO LITTER, DUAL CONTROLS INSTALLED", 2224.41, 3479.41],
    ["VIP SEATS, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2205.51, 3485.93],
    ["ONE LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2209.51, 3485.09],
    ["TWO LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2220.01, 3482.33],
    ["FLOATS, RAFT, VESTS REMOVED", 2084.51, 3507.26]
];

// values for AC N902AH
/////////////////////////////////////////////////////////// 
var N902AH = [
    ["Select a Configuration2", "Select Config", "Select Config"],
    ["6 PAX STANDARD SEATS OFFSHORE", 2205.61, 3485.69],
    ["DUAL CONTROLS INSTALLED", 2207.81, 3483.76],
    ["VIP SEATS INSTALLED", 2212.11, 3483.75],
    ["VIP SEATS, DUAL CONTROLS INSTALLED", 2214.31, 3481.83],
    ["ONE LITTER KIT INSTALLED", 2213.91, 3483.50],
    ["ONE LITTER, DUAL CONTROLS INSTALLED", 2216.11, 3481.58],
    ["6 PAX STANDARD SEATS, VESTS REMOVED", 2196.81, 3489.83],
    ["VIP SEATS INSTALLED, VEST REMOVED", 2203.31, 3487.87],
    ["ONE LITTER INSTALLED, VESTS REMOVED", 2207.31, 3487.02],
    ["TWO LITTER INSTALLED", 2222.21, 3481.32],
    ["TWO LITTER, DUAL CONTROLS INSTALLED", 2224.41, 3479.41],
    ["VIP SEATS, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2205.51, 3485.93],
    ["ONE LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2209.51, 3485.09],
    ["TWO LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2220.01, 3482.33],
    ["FLOATS, RAFT, VESTS REMOVED", 2084.51, 3507.26]
];

// values for AC N903AH
/////////////////////////////////////////////////////////// 
var N903AH = [
    ["Select a Configuration3", "Select Config", "Select Config"],
    ["6 PAX STANDARD SEATS OFFSHORE", 2205.61, 3485.69],
    ["DUAL CONTROLS INSTALLED", 2207.81, 3483.76],
    ["VIP SEATS INSTALLED", 2212.11, 3483.75],
    ["VIP SEATS, DUAL CONTROLS INSTALLED", 2214.31, 3481.83],
    ["ONE LITTER KIT INSTALLED", 2213.91, 3483.50],
    ["ONE LITTER, DUAL CONTROLS INSTALLED", 2216.11, 3481.58],
    ["6 PAX STANDARD SEATS, VESTS REMOVED", 2196.81, 3489.83],
    ["VIP SEATS INSTALLED, VEST REMOVED", 2203.31, 3487.87],
    ["ONE LITTER INSTALLED, VESTS REMOVED", 2207.31, 3487.02],
    ["TWO LITTER INSTALLED", 2222.21, 3481.32],
    ["TWO LITTER, DUAL CONTROLS INSTALLED", 2224.41, 3479.41],
    ["VIP SEATS, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2205.51, 3485.93],
    ["ONE LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2209.51, 3485.09],
    ["TWO LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2220.01, 3482.33],
    ["FLOATS, RAFT, VESTS REMOVED", 2084.51, 3507.26]
];

// values for AC N904AH
/////////////////////////////////////////////////////////// 
var N904AH = [
    ["Select a Configuration", "Select Config", "Select Config"],
    ["6 PAX STANDARD SEATS OFFSHORE", 2205.61, 3485.69],
    ["DUAL CONTROLS INSTALLED", 2207.81, 3483.76],
    ["VIP SEATS INSTALLED", 2212.11, 3483.75],
    ["VIP SEATS, DUAL CONTROLS INSTALLED", 2214.31, 3481.83],
    ["ONE LITTER KIT INSTALLED", 2213.91, 3483.50],
    ["ONE LITTER, DUAL CONTROLS INSTALLED", 2216.11, 3481.58],
    ["6 PAX STANDARD SEATS, VESTS REMOVED", 2196.81, 3489.83],
    ["VIP SEATS INSTALLED, VEST REMOVED", 2203.31, 3487.87],
    ["ONE LITTER INSTALLED, VESTS REMOVED", 2207.31, 3487.02],
    ["TWO LITTER INSTALLED", 2222.21, 3481.32],
    ["TWO LITTER, DUAL CONTROLS INSTALLED", 2224.41, 3479.41],
    ["VIP SEATS, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2205.51, 3485.93],
    ["ONE LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2209.51, 3485.09],
    ["TWO LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2220.01, 3482.33],
    ["FLOATS, RAFT, VESTS REMOVED", 2084.51, 3507.26]
];

// values for AC N905AH
/////////////////////////////////////////////////////////// 
var N905AH = [
    ["Select a Configuration", "Select Config", "Select Config"],
    ["6 PAX STANDARD SEATS OFFSHORE", 2205.61, 3485.69],
    ["DUAL CONTROLS INSTALLED", 2207.81, 3483.76],
    ["VIP SEATS INSTALLED", 2212.11, 3483.75],
    ["VIP SEATS, DUAL CONTROLS INSTALLED", 2214.31, 3481.83],
    ["ONE LITTER KIT INSTALLED", 2213.91, 3483.50],
    ["ONE LITTER, DUAL CONTROLS INSTALLED", 2216.11, 3481.58],
    ["6 PAX STANDARD SEATS, VESTS REMOVED", 2196.81, 3489.83],
    ["VIP SEATS INSTALLED, VEST REMOVED", 2203.31, 3487.87],
    ["ONE LITTER INSTALLED, VESTS REMOVED", 2207.31, 3487.02],
    ["TWO LITTER INSTALLED", 2222.21, 3481.32],
    ["TWO LITTER, DUAL CONTROLS INSTALLED", 2224.41, 3479.41],
    ["VIP SEATS, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2205.51, 3485.93],
    ["ONE LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2209.51, 3485.09],
    ["TWO LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2220.01, 3482.33],
    ["FLOATS, RAFT, VESTS REMOVED", 2084.51, 3507.26]
];

// values for AC N907AH
/////////////////////////////////////////////////////////// 
var N907AH = [
    ["Select a Configuration", "Select Config", "Select Config"],
    ["6 PAX STANDARD SEATS OFFSHORE", 2205.61, 3485.69],
    ["DUAL CONTROLS INSTALLED", 2207.81, 3483.76],
    ["VIP SEATS INSTALLED", 2212.11, 3483.75],
    ["VIP SEATS, DUAL CONTROLS INSTALLED", 2214.31, 3481.83],
    ["ONE LITTER KIT INSTALLED", 2213.91, 3483.50],
    ["ONE LITTER, DUAL CONTROLS INSTALLED", 2216.11, 3481.58],
    ["6 PAX STANDARD SEATS, VESTS REMOVED", 2196.81, 3489.83],
    ["VIP SEATS INSTALLED, VEST REMOVED", 2203.31, 3487.87],
    ["ONE LITTER INSTALLED, VESTS REMOVED", 2207.31, 3487.02],
    ["TWO LITTER INSTALLED", 2222.21, 3481.32],
    ["TWO LITTER, DUAL CONTROLS INSTALLED", 2224.41, 3479.41],
    ["VIP SEATS, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2205.51, 3485.93],
    ["ONE LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2209.51, 3485.09],
    ["TWO LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2220.01, 3482.33],
    ["FLOATS, RAFT, VESTS REMOVED", 2084.51, 3507.26]
];
//alert(eval('N901AH'));
var subListValue="";
$( document ).ready(function() {
//alert(aircraft.length);

   for(i=0;i<aircraft.length;i++)
   {
	
	 $('#list1')
         .append($("<option></option>")
         .attr("value",aircraft[i][0])
         .text(aircraft[i][0]));
		 
		
   }
   
   
   $('#list1').on('change', function() {
   $('#list2').empty();
  subListValue=eval(this.value ); // or $(this).val()
  for(i=0;i<subListValue.length;i++)
   {
	
	 $('#list2')
         .append($("<option></option>")
         .attr("value",subListValue[i][0])
         .text(subListValue[i][0]));
		 
		
   }
});
});
</script>
<select id="list1">
</select>
<select id="list2">
</select>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.