I need to use a function like this for multiple times with different ids based on different conditions (e.g. select values). I don't want to write a different function for every id because there will be lots of them in the future.
var getValue = function(id){
var Element = document.getElementById(id);
if(Element){
if(Element.value){
return " " + Element.value;
}
return "";
}
return "";
}
I have an array including some other functions:
FunctionList = [ SomeFunction, SomeFunction2 ];
Based on some conditions, I add getValue function with different parameters to this array.
FunctionList.push(getValue(SomeId1));
FunctionList.push(getValue(SomeId2));
FunctionList.push(getValue(SomeOtherId));
In the end, I need to add outputs of my functions (which are strings) to another string.
var updateCode = function(){
var code = CodeHeader;
for (var i=0; i<FunctionList.length; i++){
code += FunctionList[i]();
}
return code;
}
But I am unable to use it like this since some items of array are not function names now. I like pure js solutions, but if it's not possible I can go for jQuery or so.