0

Here is my HTML code:

<button id="remove10" onclick="subtract()">Remove 10</button>

<div id="final-answer">Final answer</div>

Here is my Javascript code:

var startingNumber = 100;
var minus10 = document.querySelector("#remove10");

function subtract() {
  startingNumber = startingNumber - 10;
}

minus10.onclick = subtract();

document.querySelector("#final-answer").innerHTML = startingNumber;

My goal is that every time the "Remove 10" button is clicked it subtracts 10 from the initial value of 100 and changes the displayed text to reflect the answer. So 1st click - show 90, 2nd click - show 80, etc.

The code I currently have shows the number as 90 from the start and nothing happens when I click the button. What am I doing wrong here?

  • You'll need to move the `innerHTML` assignment into the `subtract` function as well, so that it runs every time `startingNumber` is updated not only once in the beginning. – Bergi Jan 12 '20 at 22:39

3 Answers3

2

There are a couple of issues with your code:

  1. You don't need to assign an onclick handler in both HTML and JS, one is fine. JS is the more common choice.
  2. When assigning an event handler in JS, you must provide a function. subtract() calls the function and assigns its result (which is nothing) to onclick.
  3. You need to update HTML each time subtract is called. The browser doesn't automatically bind startingNumber to the innerHTML of #final-answer.

The code could look like this

var startingNumber = 100;
var minus10 = document.querySelector("#remove10");

function subtract() {
  startingNumber = startingNumber - 10;
  document.querySelector("#final-answer").innerHTML = startingNumber;
}

// you have to assign a function to "onclick", subtract() calls the function and  returns nothing
minus10.onclick = subtract;

document.querySelector("#final-answer").innerHTML = startingNumber;
<button id="remove10">Remove 10</button>

<div id="final-answer">Final answer</div>
pablochan
  • 5,625
  • 27
  • 42
1

I change little your code and add notes inside. Hope that will help you:

var startingNumber = 100; // this is the original number
  document.querySelector("#final-answer").textContent = startingNumber; // show it in the container
var minus10 = document.querySelector("#remove10"); // this is the button 
minus10.addEventListener('click', subtract); // when clicking the button a function will execute
function subtract() { // this is the function that will execute
  startingNumber = startingNumber - 10; // get the number and then subtract 10 from it
  document.querySelector("#final-answer").textContent = startingNumber; // write the new number
}
<button id="remove10">Remove 10</button>
<div id="final-answer">Final answer</div>

More side notes:

  1. Instead onclick i use the event listener - read why.

  2. Instead innerHTML i used the textContent - read why.

Enjoy Code!

A. Meshu
  • 4,053
  • 2
  • 20
  • 34
0

You aren't assigning the function subtract to onclick, instead you are calling subtract and assigning the value to onclick

Instead try

minus10.onclick = subtract;

You will also need to update innerHtml in your subtract function too, as currently it's only setting it once.

Connor Burton
  • 419
  • 1
  • 5
  • 15