0

I am making an idle game and want something to be purchased on user click. I have an onclick event connected to it which triggers the buyCreateScript() function but it is not working. Any advice? Fiddle:https://jsfiddle.net/wizviper/m1ftgoyp/82/ Javascript:

function buyCreateScript() {
if(bytes >= createScriptCost ) {
   createScriptAmount++;
   bytes = bytes - createScriptCost;
   createScriptCost = createScriptCost * priceIncrease;
  }
}

HTML:

   <button type="button" class="btn-primary" id="createScript" 
 onclick="buyCreateScript()">Create Script-0</button>
wizviper
  • 19
  • 5

2 Answers2

0

You need to change how you load the javascript in your fiddle.

  • Click on the javascript options cog icon
  • Change "LOAD TYPE" to "No wrap - in < head>"
  • Click "Run" and try again
itodd
  • 2,278
  • 1
  • 14
  • 14
0

The solution is to either:

  1. Keep the onclick attribute but add a script tag in the html section and write the function buyCreateScript() in it:

<button type="button" class="btn-primary" id="createScript" onclick="buyCreateScript()">Create Script-0</button>
<script>
  function buyCreateScript() {
    console.log('works');
  }
</script>
  1. Or, you could add an event listener in the javascript section, remove the onclick attribute and keep your function where it is:

var createScriptBtn = document.getElementById('createScript');
createScriptBtn.addEventListener('click', function() {
  buyCreateScript();
});
function buyCreateScript() {
  console.log('works');
}
<button type="button" class="btn-primary" id="createScript">Create Script-0</button>
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55