user7718111
user7718111

Reputation:

Calling a method from an Object as an event listener

Ok i'm making a Javascript Cash Register and have included a button. When I click the button I want it to call a function. This is how I have gone about it:

var clear = document.getElementById("clearSpend");//the button
clear.addEventListener("click",  procedures.clearSpend(), false);

I have an object named "procedures" and inside it I have a method "clearSpend" which resets the total to 0 (irrelevant I know). Whenever I run the program I receive "Uncaught TypeError: Cannot read property 'clearSpend' of undefined".

Upvotes: 1

Views: 35

Answers (1)

Pavlo Zhukov
Pavlo Zhukov

Reputation: 3337

You must setup a link to your function, but not result of it's execution

var procedures = {clearSpend:function(){alert('clearSpend:function');}};
var clear = document.getElementById("clearSpend");//the button
clear.addEventListener("click",  procedures.clearSpend, false);
<div id="clearSpend" style="width:220px;height:120px;background-color:red;"></div>

Upvotes: 1

Related Questions