jQuery ajax: Load and fadeIn

This is an external html (external.html) I want to load into a page with ajax,

<div id="wrapper">
   <div id="contents">

This is the parent page,

        <li><a href="#" onclick="ajax_request(); return false;">1</a></li>

<div class="bottom" id="studies">

This is the ajax call which I am using Jquery,

function ajax_request() {

I can load the external.html successfully, but I am not able to fade in the div with the id of ‘contents’ because it is inside the external.html

How do I get around to it?

Many thanks,

Ajax is asynchronous, which means once you make the call to fetch something, javascript doesn’t sit there an wait until its finished. It continues executing, and just does the download in the background, finishing it whenever it can.

This means the line of code following your call to load() executes immediately, which is probably well before anything is actually loaded. You need to wait until the data is loaded before trying to fade it in. Since you can’t really guess how long it will take for the loading to complete, load() accepts a callback function which jquery will execute once the operation is complete.


function iAmACallbackFunction() {

$('#studies').load("external.html", {}, iAmACallbackFunction);

Got it, many thanks for this. It works perfectly on my site.:smiley: