2

I have looked at numerous questions already on here and I haven't been able to find a similar problem. I am trying to give a button a function that updates a variable (specifically a number, i.e. - button press >> 0 becomes 1) and write it to the innerHTML of a div. It updates in an alert, but it doesn't update the innerHTML.

var example = 0;
var exampleDiv = document.getElementById('exampleDiv');

function buyExample() {
  example += 1;
}

exampleDiv.innerHTML = 'You have ' + example + ' examples.';

The number displayed on the webpage is 0.

5
  • 3
    Can you provide your html code? Commented Jul 31, 2017 at 21:13
  • 1
    You probably want exampleDiv.innerHTML = ... to be inside the buyExample function, but you haven't really shared enough code for anyone to be sure. Commented Jul 31, 2017 at 21:15
  • you have to provide the whole code to be sure, but you can't expect that exampleDiv.innerHTML is updated just because you have incremented "example". Most probably, you also have to set exampleDiv.innerHTML inside the buyExample() function. Commented Jul 31, 2017 at 21:15
  • You never invoke buyExample, so it is never increased. Commented Jul 31, 2017 at 21:17
  • exampleDiv.innerHTML = 'You have ' + example + ' examples.'; goes inside buyExample() function. Commented Jul 31, 2017 at 21:18

4 Answers 4

1

Your code is not working properly since exampleDiv.innerHTML is set only once. You have to move it to the function.

var example = 0;
var exampleDiv = document.getElementById('exampleDiv');
render();

function buyExample() {
  example += 1;
  render();
}

function render() {
  exampleDiv.innerHTML = 'You have ' + example + ' examples.';
}
<button id="button" onclick="buyExample()">Button</button>

<div id="exampleDiv"></div>

Sign up to request clarification or add additional context in comments.

Comments

0

Working code based on my comment above:

var example = 0;
var exampleDiv = document.getElementById('exampleDiv');

function buyExample() {
  example += 1;
  exampleDiv.innerHTML = 'You have ' + example + ' example' + (example !== 1 ? 's' : '') + '.';
}

document.getElementById("incrementButton").onclick = buyExample;
<button id="incrementButton">Click me</button>

<div id="exampleDiv">You have 0 examples.</div>

Comments

0

From the context you've given, your statement:

exampleDiv.innerHTML = 'You have ' + example + ' examples.';

Will only run once when the Javascript initializes, at which point the example variable is still set to 0. If you want the div to update every time you use the button, it needs to be included in the buyExample() method as well.

function buyExample() {
    example += 1;
    exampleDiv.innerHTML = 'You have ' + example + ' examples.';
}

Comments

0

You have to move the statement that updates the DOM into the function that runs each time the button is clicked. The way you have it, the innerHTML is set once when the page loads and the variable example gets updated on each click, but you never set the value of example to the page again.

var example = 0;
var exampleDiv = document.getElementById('exampleDiv');

function buyExample() {
  example += 1;
  exampleDiv.innerHTML = 'You have ' + example + ' examples.';
}
<div id="exampleDiv">You have 0 examples.</div>
<button onclick="buyExample()">buy</button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.