daffron
daffron

Reputation: 1

Variable value not accumulating?

Hey I'm very new to JS but cannot figure out why the 3 variables at the top aren't accumulating?

var xe = 0;
var reckon = 0;
var books = 0;


document.getElementById('#go').addEventListener('click', function(){
questionOne();
});
document.getElementById('#macos').addEventListener('click',function(){
  addXe();
  questionThree();
});
document.getElementById('#windows').addEventListener('click',function(){
  questionThree();
});
document.getElementById('#linux').addEventListener('click', function(){
  questionThree();
});
document.getElementById('#large').addEventListener('click', function(){
  questionFour();
  addXe();
});



function addXe(){
  xe++;
};
function addReckon(){
  reckon++;
};
function addBooks(){
  books++;
};

The rest of the code is simply a series of functions that toggles divs display on and off. If Ive missed and important info out let me know, Thanks!

EDIT:

github.com/daffron/accountingsoftware

I thought it would be easier to attach the whole site, the problem is each time the addXero function is executed , it doesn't accumulate the value of var xero, I have it printing to console in the other functions. Thanks –

Upvotes: 0

Views: 78

Answers (2)

Rahul Bharadwaj
Rahul Bharadwaj

Reputation: 2784

Your parameters for document.getElementById() are wrong. If your HTML code for a button is like this:

<input type="button" id="go" />
<!-- OR like this: -->
<button id="go" ></button>

Then your javascript code must access the button using the method getElementById() as:

document.getElementById("go");

So, one of your functions will be like:

document.getElementById('go').addEventListener('click', function(){
questionOne();
});

BUT, if you've used '#go' because you were referring to a CSS Selector then you've to use querySelector method instead of getElementByIdas:

document.querySelector("#go");

So, a function call as per your code will be:

  document.querySelector('#go').addEventListener('click', function(){
    questionOne();
    });

Upvotes: 3

coenni
coenni

Reputation: 422

you should have html label example

<label id="xeLbl">test</label>

and after changing value you should apply it to html label.

function addXe(){
  xe++;
  document.getElementById('xeLbl').innerHTML = xe;
};

Upvotes: 0

Related Questions