Reputation: 2471
I have a multidimensional array in which some values are present i
want to retrieve the [0][1]
or [1][1]
index value. I am getting
undefined as array, if i tried to directly try to get the array value
am able to get the value.
This what i want to achieve
I had a select drop down menu , According to the selected index i
need to retrieve a message from the array box. For say if the index is
1 then i had to get [1][1]
array index value if it is zero then [0][1]
array index value
This is the fiddle what i have done. http://jsfiddle.net/hTQZ9/
Upvotes: 0
Views: 177
Reputation: 665574
You should really use an array literal instead of, er, cntr
s:
var testName_MessagesArray = [
["testName_custom_message_Label1val", "Custom message for label 1"],
["testName_custom_message_Label2val", "Custom message for label 2"]
];
Then, if you want to retrieve a value from it, use testName_MessagesArray[x][y]
.
What you were doing:
var classChk=$(".customCheckEnabled");
// this is a jQuery element, alerted as [object Object]
var getClassindex=classChk.attr("selectedIndex");
// this is 0 or 1
var getVarName=classChk.attr("id");
// this will be the id of the first selected element, a string
var getCstMsgName=getVarName+"_MessagesArray".toString();
// this will create a string, from the "getVarName"-string and your string-literal-toString
var getMessage=getCstMsgName[getClassindex][1];
// as "getCstMsgName" is a string - not the twodimensional array,
// getCstMsgName[getClassindex] returns the character at the selected index (as a string)
// and getCstMsgName[getClassindex][1] results in the second character of the one-character-string - undefined
Upvotes: 0
Reputation: 207557
getCstMsgName is a string, not an array.
One way is to use this
$(document).ready(function () {
var testName_MessagesArray = new Array();
var cntr = 0;
testName_MessagesArray[cntr] = new Array("testName_custom_message_Label1val", "Custom message for label 1");
cntr++;
testName_MessagesArray[cntr] = new Array("testName_custom_message_Label2val", "Custom message for label 2");
cntr++;
alert(testName_MessagesArray[1][1]);
var classChk = $(".customCheckEnabled");
alert(classChk);
this.testName = testName_MessagesArray; //<-- set this with the name
var getClassindex = classChk.attr("selectedIndex");
alert(getClassindex);
var getVarName = classChk.attr("id");
alert(getVarName);
var getCstMsgName = this[getVarName]; //<-- reference it from this
alert(getCstMsgName);
var getMessage = getCstMsgName[getClassindex][1];
alert(getMessage);
});
If testName_MessagesArray
is in global scope, you can do window["testName_MessagesArray"]
to reference it. Your current example is local scope so that would not work.
Upvotes: 0
Reputation: 123438
see this update: http://jsfiddle.net/hTQZ9/1/
var MessagesObj = {
testName: []
}
MessagesObj["testName"].push(["testName_custom_message_Label1val","Custom message for label 1"]);
MessagesObj["testName"].push(["testName_custom_message_Label2val","Custom message for label 2"]);
alert(MessagesObj["testName"][1][1]);
var classChk = $(".customCheckEnabled").get(0);
var getClassindex = classChk.selectedIndex;
var getVarName = classChk.id
var getCstMsgName = MessagesObj[getVarName];
alert(getCstMsgName);
var getMessage = getCstMsgName[getClassindex][1];
alert(getMessage);
Upvotes: 1