I'd like to include an onclick event in a JavaScript class, but since onclick is a function inside of the class the this
variable won't work properly.
How can I modify/output the this
variable from the onclick function?
img = new image();
function image() {
this.width = 400;
this.height = 600;
document.getElementById('button').onclick = function()
{
alert(this.width); // alerts undefined
};
}
See JSfiddle here: http://jsfiddle.net/ZghRv/