0

I am creating a series of buttons dynamically. How can I add a parameter to the Event Listener this.Purchase below as follows: this.Purchase(i)? Simply adding "(i)" does not work.

      for (var i = 0; i < size; i++) {
         var button = document.createElement('input')
         button.setAttribute('type', 'submit')
         button.setAttribute('value', 'Purchase')
         button.addEventListener('click', this.Purchase)
      }
3
  • 1
    () => this.Purchase(i) Commented Nov 18, 2020 at 22:57
  • A better approach though would be to button.setAttribute('data-index', i) and use that from within the event handler instead. Commented Nov 18, 2020 at 22:58
  • Taplar, I am not sure I understand how to use the 'data-index' attribute within the event handler. Thanks again. Commented Nov 18, 2020 at 23:13

2 Answers 2

1

var thing = {
  Purchase: function() {
    console.log(this.getAttribute('data-index'));
  }
};

for (var i = 0; i < 3; i++) {
  var button = document.createElement('input');
  button.setAttribute('type', 'submit');
  button.setAttribute('value', 'Purchase');
  button.setAttribute('data-index', i);
  button.addEventListener('click', thing.Purchase);
  
  document.body.appendChild(button);
}

An example of using a data attribute with the event handler, as I mentioned in the comments.

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

Comments

0

Add a data attribute to each button, and call the purchase function in the onclick callback using the this keyword:

var size = 3

for (var i = 0; i < size; i++) {
  var button = document.createElement('input')
  button.setAttribute('type', 'submit')
  button.setAttribute('value', 'Purchase')
  button.dataset.idx = i
  button.addEventListener('click', function() {
    purchase(this.dataset.idx)
  })
  document.body.appendChild(button)
}

function purchase(idx) {
  console.log(idx)
}

6 Comments

If you set it as a dataset on the element processing the event, you don't have to pass it in. Thats part of the whole reason of having it as a dataset element.
Thank you for your time symlink. However, Taplar's solution stated in the comments above works perfectly.
@Taplar if you want to call a separate function in your event handler, though, you have to pass this or an attribute of this to the function.
addEventListener('click', this.Purchase) as the OP had it is calling another method when the click happens. Since the function is called via the event handler attached to the button clicked, the this inside the execution of the Purchase method will be the button clicked, not the element the function was originally attached to.
@symlink, I have tried your solution but it is not working. Upon clicking on any button in my "array" of buttons, I always get the last value as the value of the parameter passed (same actually for solutions provided by Taplar). The value of the variable is not "fixed" on the button during its creation, but passed at the time when it is actually clicked (which always corresponds to the value it had at the end of the for loop). Thank you.
|

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.