I have built a js class that is have the control (html Control ) parameter, I tried to add dynamically an onchange event to the control but I have the following error:
htmlfile: Not implemented
//-------------- the code
Contrl.prototype.AddChangeEvent = function() {
var element = this.docID;
var fn = function onChange(element) {
// action
};
if (this.tag == "input" && (this.el.type == "radio")) {
this.el.onclick = fn(element); // there i have the error
}
else {
this.el.onchange = fn(element); // there i have the error
}
}
By writing
this.el.onclick = fn(element), you’re callingfnimmediately, and assigning whateverfnreturns toonclick.You need to make an anonymous function that calls
fnwith the arguments you want it to get, like this:However, this is not the correct way to assign event handlers in Javascript.
You should call
attachEvent(for IE) oraddEventListener(for everything else), like this: