0

Say I have four buttons, set out like so:

Click me (button 1) - Or Click me (button 2)

option 1(button 3) - option 2 (button 4)

If I click on button 1 or 2, I want a local variable to be set to which button was pressed.

When I click on either button 3 or 4, I need to access the variable that holds which button was clicked and process it.

I would like to avoid creating variables in the global scope or using more than one. Is this even possible?

What I've Tried:

function setButtonId(i) {
  this.id = i
}
setbuttonId.process = item => {
  console.log(item)
}
//tmpcode
4
  • 1
    you know, I don't just ask questions to annoy the community, right? I ask questions because I need help, and I can't get unstuck. Commented Apr 8, 2018 at 11:08
  • Don't forget the new operator. Commented Apr 8, 2018 at 11:08
  • Kinda hoping to avoid that, but yes, it would work Commented Apr 8, 2018 at 11:09
  • Then instead of this.id = 1 use return {id: 1}. Commented Apr 8, 2018 at 11:09

2 Answers 2

1
   const logic = {
    id: null,
    setId(i) {
     this.id = i;
    },
    process() {
     console.log(this.id);
    }
 };

And then use it as:

  button1.onclick = () => logic.setId(3);
  button4.onclick = () => logic.process();

But i would actually prefer a non-global outer scope variable:

 {
   let id = null;
   function setId(i){ id = i}
   function process() {
     console.log(id);
   }
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the straight answer. It worked very well.
1

If you want to avoid global variables then you can either use immediate invoked function expression:

(function() {
  var lastId;
  document.getElementById("1").addEventListener('click', () => {
    lastId = 1;
  });

  document.getElementById("2").addEventListener('click', () => {
    lastId = 2;
  });

  document.getElementById("3").addEventListener('click', () => {
    console.log(lastId)
  });

  document.getElementById("4").addEventListener('click', () => {
    console.log(lastId)
  });
}())
<button id="1">1</button><button id="2">2</button><button id="3">3</button><button id="4">4</button>

or let:

{
  let lastId;
  document.getElementById("1").addEventListener('click', () => {
    lastId = 1;
  });

  document.getElementById("2").addEventListener('click', () => {
    lastId = 2;
  });

  document.getElementById("3").addEventListener('click', () => {
    console.log(lastId)
  });

  document.getElementById("4").addEventListener('click', () => {
    console.log(lastId)
  });
}
<button id="1">1</button><button id="2">2</button><button id="3">3</button><button id="4">4</button>

1 Comment

Awesome, thank you! such an approach, I would not have come up with

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.