Doesn't add a click listener to a newly added element?

Doesn't add a click listener to a newly added element?

My jQuery:

  1. $(document).ready(function(){
  2.     var search = true;
  3.     var results = $('#results');
  4.     var input = $('input[name="username"]');
  5.     var viewing = 0;
  6.     
  7.     $('button[id|="user"]').click(function(){
  8.         document.write('hey');
  9.     });
  10.     
  11.     input.keypress(function(){
  12.         if(search){
  13.             search_f();
  14.         }
  15.     });
  16.     
  17.     $('#back').click(function(){
  18.         if(!search){
  19.             window.location.hash=viewing;
  20.         }
  21.     });
  22.     
  23.     function search_f(){
  24.         if(search){
  25.             $('label[for="username"]').html('<img src="../../img/loading.gif" width="20" height="13">');
  26.             if(input.val().length > 0){
  27.                 $.ajax({
  28.                     url : 'infract_search.php',
  29.                     type : 'post',
  30.                     data : { username : input.val() }
  31.                 }).done(function(r){
  32.                     results.html(r);
  33.                     $('label[for="username"]').html('');
  34.                 });
  35.             }
  36.         }
  37.     }
  38.     search_f();
  39. });

As you can see, when a username is typed into the field, it goes to infract_search.php and grabs the results. The results it grabs is an HTML table that returns a list of usernames. The PHP:


I've double checked each button in FireBug and they have their prefix and suffixes correctly. E.g:

<button id="user-14"
<button id="user-308"
<button id="user-99"

However, it seems this line of code in my above jQuery script:

  1. $('button[id|="user"]').click(function(){
  2.         document.write('hey');
  3. });

Doesn't work for some reason. Is this because the HTML retrieved from the AJAX requests isn't seen by the function? If so, how can I go about solving that?