0

I'm using the code below on my content_script to get mouse target, however, it doesn't get the 'click' trigger on a dropdown with elements created at runtime. By that I mean, I can see the target of everything, even the dropdown itself, just the elements created are not triggering the function.

$(window).on('click', function(event){
{
console.log(event.target);
}

good to mention mousemove works fine

document.onmousemove = function(e)
{
console.log(e.target);
}

my dropdown is basically a bunch of 'li' inside a 'ul' element, created from another js, displayed as a dropdown, does anyone have any ideas?

1 Answers1

0

The event binding code must be executed when the element it binds to is already there. You need to use Event Delegation with.on() Same question answered here example:

$(document).on('.select2-results__options .select2-results__option','selector', function(event){
  console.log(event.target);
});
Zahema
  • 1,345
  • 2
  • 19
  • 35
  • I am already using .on() and the selector haven't made any change, I can indeed click on the search bar created dynamically on the top of my dropdown, however, the li elements does not seem to trigger the click function – Felipe Amorim Oct 29 '18 at 04:18
  • @FelipeAmorim Can you share your code in a pen so we get a better look at it? – Zahema Oct 29 '18 at 10:40
  • As the source code and the source code compiled with the js are different, follows on the image link, the source code compiled http://prntscr.com/lbsy5o – Felipe Amorim Oct 29 '18 at 13:20