Button alert function not working when loaded with ajax

Button alert function not working when loaded with ajax

I have a search form in my page1.php file, which is targeted to page2.php, which displays results. I have a button in my page2.php, which on click, alerts "hi", like;

  1. $(document).ready(function() {
  2.     $('#alertbtn').click(function(){
  3.         alert("hi");
  4.     });
  5. });

This works fine when page2.php is loaded separately, without using page1.php's search form. When I search using page1.php's form, and when page2.php appears, the alert function is not working. How can I fix this?