I have defined a class in JavaScript with a single method:
function MyClass(text) {
this.text = text;
}
MyClass.prototype.showText = function() {
alert(this.text);
}
Then, I defined a method that acts as a handler for a click event, 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 click the button, it fails saying:
Object #<HTMLInputElement> has no method 'showText'
It seems to be that this in jQuery click event handler refers the HTML element itself, and it does not refer the instance of the MyClass object.
How can I solve this situation?
jsFiddle available: http://jsfiddle.net/wLH8J/