Reputation: 265
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
}
}
Upvotes: 0
Views: 705
Reputation: 887469
By writing this.el.onclick = fn(element)
, you're calling fn
immediately, and assigning whatever fn
returns to onclick
.
You need to make an anonymous function that calls fn
with the arguments you want it to get, like this:
this.el.onclick = function() { return fn(element); };
However, this is not the correct way to assign event handlers in Javascript.
You should call attachEvent
(for IE) or addEventListener
(for everything else), like this:
function bind(elem, eventName, handler) {
if (elem.addEventListener)
elem.addEventListener(eventName, handler, false);
else if (elem.attachEvent)
elem.attachEvent("on" + eventName, handler);
else
throw Error("Bad browser");
}
Upvotes: 1