I'm trying to execute a method that's inside an object when you click a button, but apparently, it doesn't run. What am I doing wrong? I saw the variable "price" on Chrome's console, and it says "undefined" before and after pressing the button.
Javascript code:
function Upgrade () {
this.buy = function(){
this.price = 40;
};
}
var upg = new Upgrade();
var button = document.getElementById("button");
button.onclick = upg.buy;
HTML code:
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<div>TODO write content</div>
<button id="button" type="button">Upgrade 1</button>
<script src='newjavascript.js'></script>
</body>
</html>