0

I am working on a jquery form file that uses an array to populate a dropdown of sizes based on the selection of the material. This works fine but now we are adding an additional dropdown and we need to have different values in the array based on that selection.

this is the part of the current working code:

var Material = new Array();
Material['UNC']    = new Array('40', '32');
Material['UNF']    = new Array('10', '24');

This is basically what I am trying to, not sure how:

if($("#style").val() == "long") {  var Material = new Array();
Material['UNC']    = new Array('45', '35');
Material['UNF']    = new Array('15', '29');} else {

var Material = new Array();
Material['UNC']    = new Array('40', '32');
Material['UNF']    = new Array('10', '24');}

I'm not having any luck, I'm not super familiar with Javascript functions. Thanks

4
  • Don't use arrays with non-numerical keys. Use objects instead. Learn more about objects: developer.mozilla.org/en-US/docs/JavaScript/Guide/…. Commented Feb 21, 2013 at 2:35
  • Also better to use array literals than the constructor. What do you expect from new Array(10);? Commented Feb 21, 2013 at 2:36
  • Are all values for all possible lists known at the time the page loads or are you going back to your sever to fetch a list of sub dropdown values? Commented Feb 21, 2013 at 2:36
  • Thank you for all the help, I'm still having an issue and I think it is because the value is not really being determined unless the selection is submitted. Is it possible to use something like if($("#style").val() == "long") but just having it selected on the dropdown on the same page, not yet submitted? Commented Feb 21, 2013 at 13:04

2 Answers 2

2

One way:

var isLong = $('#style').val() === 'long';
var material = {};
material.UNC = (isLong) ? [45, 35] : [40, 32];
material.UNF = (isLong) ? [15, 29] : [10, 24];

Another way:

var isLong = $('#style').val() === 'long';
var material = {};
if (isLong) {
  material.UNC = [45, 35];
  material.UNF = [15, 29];
}
else {
  material.UNC = [40, 32];
  material.UNF = [10, 24];
}

As Felix Kling points out, it is better to use an object over an array for material. I've also used JavaScript convention of a lowercase variable name. Instead of using new Array use [] and instead of new Object, you can use {}.

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

Comments

0

You just need to move the declaration of the Material variable outside the blocks:

var Material = new Array();
if($("#style").val() == "long") {
    Material['UNC']    = new Array('45', '35');
    Material['UNF']    = new Array('15', '29');
} else {
    Material['UNC']    = new Array('40', '32');
    Material['UNF']    = new Array('10', '24');
}

However, as others have pointed out, you should be using an object rather than an array for these kinds of non-numeric indexes. You should also use object and array notation:

var Material = {};
if($("#style").val() == "long") {
    Material['UNC']    = ['45', '35'];
    Material['UNF']    = ['15', '29'];
} else {
    Material['UNC']    = ['40', '32'];
    Material['UNF']    = ['10', '24'];
}

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.