boomer1204 boomer1204 - 3 months ago 13
Javascript Question

addeventlistener click only working on first click

Been trying to recreate the old palm pilot dope wars game and can't figure out what part of the code is messing this up. Trying to add 1 to the value when you click the + button. Here is the code thanks!

init: function(){
this.acid = document.getElementById('acid');
this.acidQty = document.getElementById('acidQty');
this.acidAdd = document.getElementById('acidAdd');
},
// bind the events to the button
bindEvents: function(){
this.acidAdd.addEventListener('click', this.addAcid.bind(this), false);
// $("#acidAdd").on('click', this.addAcid.bind(this));
},
//Get the value
getQty: function(){
this.acidQuantity = $('#acidQty').val();
},
//get current value and add 1
addAcid: function(){
this.getQty();
this.acidQuantity ++;
this.updateQuantity();
},
// update the value on the html
updateQuantity: function(){
this.acidQty.innerText = this.acidQuantity;
},


Here is a jsbin of all the code incase I missed something else

http://jsbin.com/wakoqeduwo/1/edit?html

Answer

It is difficult to tell without seeing all the code, but I believe the following changes should work.

this.acidQuantity: 1,

bindEvents: function(){
   this.acidAdd.addEventListener.addEventListener('click', this.addAcid);
},

addAcid: function(){
  this.updateQuantity();
  this.acidQuantity ++;
},

updateQuantity: function() {
    $('#acidQty').html(app.acidQuantity); 
}