2

Can't get the GrabOldMessages() function to run after the html() runs. Below is my code. Using jQuery version 1.7.2, I think I'm calling it back correctly, am I overlooking something? Please help me find my mistake. If you need me to provide anymore info, just let me know. Thank you for taking the time to help me.

//GRAB NEW MESSAGES
function GrabNewMessages(){
    var doIB = encodeURIComponent("GET_DATA"); 
    $.ajax({ 
        type: 'POST', url: 'app/pull_data_files/inbox_NM_array.php',  dataType: "json",  data: { getInbox: doIB }, 
        success: function(inbox_NM_data) {                 
          if(inbox_NM_data[1] == 'true'){$('#inbox_NMlist_html').html(inbox_NM_data[0], function(){ GrabOldMessages(); });}
          else{alert("Didn't work");}
        }  
    }); 
return false;

}

//GRAB OLD MESSAGES
function GrabOldMessages(){

    var doIB = encodeURIComponent("GET_DATA"); 
    $.ajax({ 
        type: 'POST', url: 'app/pull_data_files/inbox_OM_array.php',  dataType: "json",  data: { getInbox: doIB }, 
        success: function(inbox_OM_data) {                 
          if(inbox_OM_data[1] == 'true'){$('#inbox_OMlist_html').html(inbox_OM_data[0], function(){GoToInbox();});}
          else{alert("Didn't work");}

        }  
   }); 
return false;
}
2
  • 2
    The problem is in the HTML callback function. Check: stackoverflow.com/questions/10300765/… Commented Aug 14, 2012 at 0:35
  • 1
    I don't think there is a Html callback function as such. The solution in the linked post was to bind the image to the load event. The function specified in the load event will be triggered as soon as the image is loaded. However, as the documentation points out: The load event is sent to an element when it and all sub-elements have been completely loaded. This event can be sent to any element associated with a URL: images, scripts, frames, iframes, and the window object. load will not work when bound to a div for example. Commented Aug 14, 2012 at 0:45

3 Answers 3

3

The .html() method doesn't take two parameters, it takes either a string or a function, not both. And when it takes a function it isn't a completion callback.

To run a function after setting the html just call the function on the next line, so change:

success: function(inbox_NM_data) {                 
    if(inbox_NM_data[1] == 'true'){
         $('#inbox_NMlist_html').html(inbox_NM_data[0], function(){ GrabOldMessages(); });
    } else{
         alert("Didn't work");}
    } 

To:

 success: function(inbox_NM_data) {                 
    if(inbox_NM_data[1] == 'true'){
         $('#inbox_NMlist_html').html(inbox_NM_data[0]);
         GrabOldMessages();
    } else{
         alert("Didn't work");}
    }

(And similar within your GrabOldMessages() function.)

Sign up to request clarification or add additional context in comments.

Comments

2

You are specifying a second parameter in html() which takes either none or one.

Try using this success version instead for your first code-snippet.

 success: function(inbox_NM_data) {
    if (inbox_NM_data[1] == 'true') {
        $('#inbox_NMlist_html').html(inbox_NM_data[0]);
        GrabOldMessages();
    }
    else {
        alert("Didn't work");
    }
}

And this for the second code-snippet:

success: function(inbox_OM_data) {
    if (inbox_OM_data[1] == 'true') {
        $('#inbox_OMlist_html').html(inbox_OM_data[0]);
        GoToInbox();
    }
    else {
        alert("Didn't work");
    }

}

Comments

-3

this code is 100% work

$('#myTabContent').html(data);    
afterHTML();

function afterHTML(){           
    $('.unit').change(function(){
        var u=($(this).val()).split('/');
        $('.perunit').html(u[1]);
    });    
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.