Doesn't add a click listener to a newly added element?
My jQuery:
- $(document).ready(function(){
- var search = true;
- var results = $('#results');
- var input = $('input[name="username"]');
- var viewing = 0;
-
- $('button[id|="user"]').click(function(){
- document.write('hey');
- });
-
- input.keypress(function(){
- if(search){
- search_f();
- }
- });
-
- $('#back').click(function(){
- if(!search){
- window.location.hash=viewing;
- }
- });
-
- function search_f(){
- if(search){
- $('label[for="username"]').html('<img src="../../img/loading.gif" width="20" height="13">');
- if(input.val().length > 0){
- $.ajax({
- url : 'infract_search.php',
- type : 'post',
- data : { username : input.val() }
- }).done(function(r){
- results.html(r);
- $('label[for="username"]').html('');
- });
- }
- }
- }
- search_f();
- });
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:
- $('button[id|="user"]').click(function(){
- document.write('hey');
- });
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?