I am trying to enable Event Handling in JavaScript. This is what I have so far:
function Field(args) {
this.id = args.id;
this.name = args.name ? args.name : null;
this.reqType = args.reqType ? args.reqType : null;
this.reqUrl = args.reqUrl ? args.reqUrl : null;
this.required = args.required ? true : false;
this.error = args.error ? args.error : null;
this.elem = document.getElementById(this.id);
this.value = this.elem.value;
this.elem.addEventListener('onblur', this, false);
this.elem.addEventListener('click', this, false);
this.elem.addEventListener('change', this, false);
console.log(JSON.stringify(this.elem.value));
}
function FormTitle(args) {
Field.call(this, args);
}
Field.prototype.getValue = function() { return Helpers.trim( this.value ) };
Field.prototype.sendRequest = function () {
};
Field.prototype.click = function (value) {
alert("click");
};
Field.prototype.onblur = function (value) {
alert("onblur");
};
Field.prototype.change = function (value) {
alert("change");
};
Field.prototype.dblclick = function (value) {
alert("dblclick");
};
Field.prototype.handleEvent = function(event) {
switch (event.type) {
case "click": this.click(this.value);
case "onblur": this.onblur(this.value);
case "change": this.change(this.value);
case "dblclick": this.dblclick(this.value);
}
};
// inheritProtootype uses parasitic inheritance to inherit from the Field's prototype
// and then assign the results to FormTitle's prototype.
inheritPrototype(FormTitle, Field);
var title = new FormTitle({name: "sa", id: "title"});
For some reason however, all events are triggered at the same time. For example, when I click on the Title field in the Form, instead of only Click event triggering, all four events are triggered.
What am I doing wrong?