views:

77

answers:

2

In the HEAD of my document I load jQuery.js and also the blockUI jQuery plugin.

In the PHP I then use regular AJAX to load other PHP content into DIVs. In the original PHP jQuery and blockUI plugin work just fine, but in any of the ajax-loaded divs jQuery and blockUI both do absolutely nothing. No console error, no warning - nothing.

I am a jQuery beginner and none of the other articles I found on this subject were able to put me over the edge of solving this, so I'm helping someone else can. In my code below you'll see I took some stabs at live()...

This is at the top of my PHP file that is loaded into the DIV

    <script type="text/javascript"> 
    $(document).ready(function() { 

        $('#crazy').live('click',function() { 
            $.blockUI({ message: $('#question'), css: { width: '275px' } }); 
        }); 

        $('#yes').live('click',function() { 
            // update the block message 
            $.blockUI({ message: "<h1>Remote call in progress...</h1>" }); 

            $.ajax({ 
                url: 'wait.php', 
                cache: false, 
                complete: function() { 
                    // unblock when remote call returns 
                    $.unblockUI(); 
                } 
            }); 
        }); 

        $('#no').live('click',function() { 
            $.unblockUI(); 
            return false; 
        }); 

    }); 
</script> 

Here is the HTML from that PHP file (loaded into the DIV):

<input id="crazy" type="submit" value="Show Dialog" /> 

<div id="question" style="display:none; cursor: default"> 
        <h1>Would you like to contine?.</h1> 
        <input type="button" id="yes" value="Yes" /> 
        <input type="button" id="no" value="No" /> 
</div> 
+1  A: 

Your document ready function loads when the DOM is loaded, before your AJAX calls complete. Thus, it only applies the .live() calls to the elements which exist before the AJAX calls.

If you want to apply things to the contents loaded by the AJAX calls, specify a callback function for the AJAX that applies the proper stuff once the loading is complete.

Amber
Hi Amber thanks for the response. I am very, very new to jQuery and this sentence "specify a callback function for the AJAX that applies the proper stuff once the loading is complete" confuses the heck out of me. What would that callback function look like and where would I place it?
http://api.jquery.com/jQuery.ajax/ Look at the `success` and/or `complete` fields in the settings argument.
Amber
The `live` event actually solves the problem of elements being added to the page via AJAX - http://api.jquery.com/live/
partkyle
thank you this got me looking in the right direction. It was the simplest thing too... My javascript above I was loading at the top of the included PHP file, not with the rest of my javascript in the HEAD of the parent file. Made the switch and now it works great with live()
Ah, right Partridge - forgot about that.
Amber
+1  A: 

What is the problem you are getting with the live version? Does it still fail silently?

Is it possible that the AJAX request in the #yes click event method is failing?

I've taken your code and simplified it a great deal on jsfiddle here, and it seems to be working fine. There isn't an issue with the usage of live and it should fix that event handler for elements being inserted via AJAX.

Have you tried just throwing alerts in the event methods to see if they get called at all?

partkyle