0

enter image description here

enter image description here

I tried to add event handler at line 166 and 167. I want the event handler executed with the parameter ``i```, whose value should be whatever when the handler was created.

Currently, if I trigger the event handler, edit(4) is executed I guess the reason is the loop terminated with i = 4.

How can I trigger event handler edit(1), edit(2), edit(3), or edit(4) based on different buttons I clicked?

updated:

for (let i = 0;i < result["wishItems"].length; i++) If I declare i in for loop, it works.

However,

let i; for (i = 0;i < result["wishItems"].length; i++) this doesn't work. Why does it matter where I declare i? Could anyone explain this? Thank you!

Thank you!

echo Lee
  • 191
  • 1
  • 2
  • 10
  • use `let` rather than `var` in the loop you have not shown in the picture – Jaromanda X Dec 06 '19 at 02:29
  • I used ```let``` but still have the same issue, – echo Lee Dec 06 '19 at 02:37
  • It works this way guys! ```for (let i = 0;i < result["wishItems"].length; i++)```. However, ```let i; for (i = 0;i < result["wishItems"].length; i++)``` this doesn't work. could anyone explain this? Thank you! – echo Lee Dec 06 '19 at 02:40

1 Answers1

0

I Think you can use class name and add event listener to the class. Call the class for the button what you want to call edit function. Or you can call the function directly to the button using on click attribute

MR. A
  • 195
  • 1
  • 11
  • it works when I used let ``` for (let i = 0;i < result["wishItems"].length; i++)```. However, if I declare ```i``` outside of for loop, I have the same issue. – echo Lee Dec 06 '19 at 02:42
  • Did you try declare i outside loop using var. Because var and let is different – MR. A Dec 06 '19 at 02:45
  • yes. when I use let and ```for(let i = 0; ....)``` it works the way I want. But it didn't work if I do ```let i = 0; for(i; ....)``` Why does it matter where I declare ```i ``` .. – echo Lee Dec 06 '19 at 03:38
  • I mean.. Declare I outside loop using var like this "var i = 0" Or "var i" – MR. A Dec 06 '19 at 03:55