HTML, Javascript – How to save variable after one click on button [closed]

I don’t really know what the code you posted is supposed to do (it’s incorrect in many ways, so it does not really make sense), so I’ll just try to help you based on what you said in your question and assume this is what you want. Tell me if it isn’t:

// Declare these variables outside of the click function
// so that you can access them the whole time
var clicks = 0,
    correct = 42,
// When the document is ready, execute `init`
window.addEventListener('DOMContentLoaded', init);

function init() {
  // When the user clicks on the button, execute `onButtonClick`
  document.getElementById('my-button').addEventListener('click', onButtonClick);

function onButtonClick() {
  // Add one to the number of clicks
  if (clicks === 1) {
    // Notice I did not use `var` here, this is because `var` makes the variable
    // local to the function, and once the function's execution is over, it's destroyed.
    // Since I declared it a the top of this script, this is the one I'm referring to.
    bott = correct;
  } else if (clicks === 3) {
    alert('bott is equal to ' + bott);
<button id="my-button">Click me!</button>

Browse More Popular Posts

Leave a Comment