0

I am working on a module which contains two pages one being the product.php and the other being the filterproducts.php

Product.php contains a Div Element which gets loaded by the .load() present inside the product.php

<div id="limitpage">
</div>

The .load() function makes an ajax call and get the output from the filterproducts.php

Structure of product.php looks like this

 <button id="nex" >clickme</button>
     <div id="limitpage">
        </div>
    <script>
          //Both of them are in document ready function
          $('#nexus').on('click',function () {

                alert("here");

            });
        $('#nex').click(function(){
           $('#limitpage').load("filterproducts.php");
        })

    </script>

filterproducts.php contains a button

<button id="nexus">Next Page</button>

But ,when the page gets loaded on('click',function()) doesn't work.Why is this exactly happening? What is the workaround to do the same,if this one is not possible.

2 Answers2

5

When the document is ready, the output from filterproducs.php is not yet present in the page, so $('#nexus') will return an empty set, and so it is not bound.

The solution is to bind something that is present in the page, and filter at the moment of the click. A typical example:

$(document).on('click', '#nexus', function() { 
  alert('nexus clicked!'); 
}) 
nathanvda
  • 48,109
  • 12
  • 108
  • 136
0

You need an event delegation. Just consider this as a replacement of .live() // deprecated now. This will bind the click function to your button

$(document).on('click', '#nexus', function() { 
  alert('nexus clicked!'); 
}) 
Tushar
  • 11,306
  • 1
  • 21
  • 41