onclick from an Object's button doesn't work
Posted
by
730
on Stack Overflow
See other posts from Stack Overflow
or by 730
Published on 2013-11-10T03:28:35Z
Indexed on
2013/11/10
3:53 UTC
Read the original article
Hit count: 99
JavaScript
I instantiate an object, with an argument which is a button. When the button of an instance is clicked, it should run a function, but it doesn't. In the full version of the code, Chrome gives this message in the console: "Uncaught TypeError: Cannot read property 'onclick' of undefined"
HTML:
<textarea id='txt' readonly rows='5' cols='40'></textarea>
<button id='btn' type='button'>click</button>
JS:
var btn = document.getElementById('btn');
var txt = document.getElementById('txt');
var foo = new Foo(btn);
function Foo(btn) {
this.button = btn;
}
Foo.prototype.buy = function() {
txt.value = 'Foo Bar';
};
Foo.button.onclick = function() {
foo.buy();
};
© Stack Overflow or respective owner