0
for (let i = 0; i < array.length; i++) {
  const aTag = $(`<div>`)
  let elem = array[i].numberID    //<--want to pass 'elem' into seeTrailer() on click
    aTag.attr('onClick', seeTrailer(elem))  //<--but this doesn't work
    trailerHref.attr('id', elem)
    ...
  }

  seeTrailer (id) => {
    const apikey=123456789;
    const queryURL = `https://url/${id}?api_key=${apikey};
    ...
  }
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • You could try this a different way, so you could set elem (which I think is an int) into a data element in your aTag and then when clicked just get it back out. – Richard Housham Feb 06 '20 at 15:07
  • Side note; you're using jQuery. Don't set inline attributes .... Use the proper `on()` method. – Taplar Feb 06 '20 at 15:10

1 Answers1

0

You can use $(<ELEM>).click() method of jQuery, and also set the ID attribute while creating the element as followings:

  let elem = array[i].numberID 
  const aTag = $(`<div/>`,{
     id: elem
  })


  aTag.click(function(){
     seeTrailer(elem)
  })
Harun Yilmaz
  • 8,281
  • 3
  • 24
  • 35