0

I have some Javascript code that executes a function whenever an element with a condition is clicked. The problem is that if a new element of that kind is added later on, it won't detect it by the code. This is my code:

$("li[data-episode]").on('click', function () {
    setTimeout(Aradmey_Later, check_timeout);
});

How can I fix it so it always works, even on future elements?

Omer Aviv
  • 286
  • 3
  • 17

2 Answers2

1

You have to use $(document) since they are dynamically added controls

$(document).on('click', "li[data-episode]", function () {
    setTimeout(Aradmey_Later, check_timeout);
});
Guruprasad J Rao
  • 28,253
  • 13
  • 87
  • 176
1

You are on the right track. But you should delegate it with a container element or object like document

$(document).on('click', 'li[data-episode]', function () {...});
AmmarCSE
  • 28,122
  • 5
  • 36
  • 49