I'm looking for a way to efficiently reuse a function multiple times with different ids depending on various conditions, such as select values. Instead of creating a new function for each id, I want a more dynamic approach to handle the potential multitude of ids 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 that contains several other functions:
FunctionList = [ SomeFunction, SomeFunction2 ];
Depending on certain criteria, I need to incorporate the getValue
function into this array with varying parameters.
FunctionList.push(getValue(SomeId1));
FunctionList.push(getValue(SomeId2));
FunctionList.push(getValue(SomeOtherId));
In the end, I must concatenate the outputs of these functions (which are strings) into another string.
var updateCode = function(){
var code = CodeHeader;
for (var i=0; i<FunctionList.length; i++){
code += FunctionList[i]();
}
return code;
}
However, I am encountering challenges using it in this manner, as some elements within the array are not function names anymore. While I prefer solutions in pure JavaScript, I'm open to using jQuery or alternative frameworks if necessary.