The following function creates input fields where the user specifies type, name and a number of classes that he would like to add for the input element. Sometimes, the user is going to want to set other attributes for the element, such as step
, or any other DOM attribute. That's why I want to use optional arguments:
function createInputField(type, name, classList, optional){
var input = document.createElement('input');
input.type = type;
input.name = name;
for(var i=0; i<classList.length; i++){
input.classList.add(classList[i]);
}
for(key,value) in optional{ // Ugly fake code. How do I implement this in Javascript?
input.getAttribute(key) = value;
}
return input;
}
input = createInputField("number", "numberfield", ["red", "top"], {"step":"0.05"})
How do I implement this functionality in Javascript?