-2

I have a jquery function for a list of links ( <a href="#" class="paging">1</a> ) that I create with $(".paging").click(.... The problem is that I call the jquery script.js on my main php file, while the links are create on a later stage.

How do I reparse my javascript file? Is there another way to do that, maybe create the jquery click function every time I create the links?

maugch
  • 1,276
  • 3
  • 22
  • 46

1 Answers1

3

You need to use event delegation:

Event delegation allows us to attach a single event listener, to a parent element, that will fire for all descendants matching a selector, whether those descendants exist now or are added in the future.

$("body").on('click','.paging',function(){
   //code here
});

Note: also it is best practice to use closest parent (instead of body selector in the above case) that exists from the beginning and does not change in future.

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125