-2

I am having trouble with my js. This is the code: https://jsbin.com/legexep/1/edit?html,js,output I'd like to get an alert box with the current number, not just to say: 'Hello'. So if I click on 607 button, I'd like the alert box to say: '607' and so on. Thank you for help.

item.addEventListener("click", function () {
alert('Hello');});
3

2 Answers 2

1

Create a callback creator function to capture loop iteration variable:

function clickCallback(value) {
  return () => alert('Hello ' + value);
}

Use within addEventListener:

item.addEventListener('click', clickCallback(array[i]));

You can also use the event object received on the callback:

item.addEventListener('click', e => alert(e.target.innerText));
Sign up to request clarification or add additional context in comments.

Comments

0

do this

item.addEventlistener("click",function() {
    alert(mgk[i]);
}

Hopefully this solves it

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.