I defined the class in JavaScript using one method:
function MyClass(text) {
this.text = text;
}
MyClass.prototype.showText = function() {
alert(this.text);
}
Then I defined a method that acts as a click event handler using jQuery:
function MyClass(text) {
this.text = text;
$('#myButton').click(this.button_click);
}
MyClass.prototype.showText = function() {
alert(this.text);
};
MyClass.prototype.button_click = function() {
this.showText();
};
When I press the button, it does not say:
Object # <HTMLInputElement> does not have a 'showText' method
It seems that thisin the jQuery event handler click refers to the HTML element itself and does not apply to the instance of the object MyClass.
How can I solve this situation?
jsFiddle is available: http://jsfiddle.net/wLH8J/
source
share