I am working for some common method in javascript, for that i have to call array dynamically.
var ddlText, ddlValue, ddl, lblMesg, ddlTextCacheList_Designation, ddlValueCacheList_Designation, ddlTextCacheList_Scale, ddlValueCacheList_Scale;
function cacheDes() {
var listDes = document.getElementById("<%=List_Designation.ClientID %>");
ddlTextCacheList_Designation = new Array();
ddlValueCacheList_Designation = new Array();
for (var i = 0; i < listDes.options.length; i++) {
ddlTextCacheList_Designation[ddlTextCacheList_Designation.length] = listDes.options[i].text;
ddlValueCacheList_Designation[ddlValueCacheList_Designation.length] = listDes.options[i].value;
}
}
function cacheScale() {
var listScale = document.getElementById("<%=List_Scale.ClientID %>");
ddlTextCacheList_Scale = new Array();
ddlValueCacheList_Scale = new Array();
for (var i = 0; i < listScale.options.length; i++) {
ddlTextCacheList_Scale[ddlTextCacheList_Scale.length] = listScale.options[i].text;
ddlValueCacheList_Scale[ddlValueCacheList_Scale.length] = listScale.options[i].value;
}
}
window.onload = function () {
cacheDes();
cacheScale();
};
I want to call array ddlTextCacheList_Scale or ddlTextCacheList_Designation for same method as we know 'ddlTextCacheList_' is common only we need to put 'Scale' or 'Designation' dynamicaaly by passing parameter.
Add:
I get some errors:
enter image description here
asked Dec 29, 2014 at 11:37
Sonam Mohite
9034 gold badges20 silver badges58 bronze badges
1 Answer 1
I suggest you to improve your cache to store all in one object to easy access...
For example:
var CacheStorage = {};
function cache(key, list) {
CacheStorage[key] = list.map(function(option){
return {text: option.text, value: option.value};
});
}
function del(key, index) {
CacheStorage[key].splice(index, 1);
}
cache('Scale', getElementByID('...').options);
cache('Designation', getElementByID('...').options);
del('Designation', 0);
answered Dec 29, 2014 at 11:55
vp_arth
15k4 gold badges43 silver badges72 bronze badges
Sign up to request clarification or add additional context in comments.
9 Comments
Sonam Mohite
just want to ask... is there need to call cache on window.onload event ?
vp_arth
It depends... it need, if your lists can be not loaded in the call time.
Sonam Mohite
yes i need. can you please tell me how to do that in this scenario
Sonam Mohite
i am asking because this cache function become parametrized now. So i dont have any idea how to call this onload
Sonam Mohite
no worries i did it. But its an exception 'JavaScript runtime error: Object doesn't support property or method 'map''
|
lang-js
window[arrName]syntax{texts: [], values: []}window['ddlValueCacheList_'+param]{Scale: [{text: '', value: 1}, {text: '', value: 2}]}