views:

912

answers:

2

Is there a callback for the html() function or a way to check that it is finished. For example:

$("#some_div").html('something here');

if($("#some_div").html() == ''){
//do something here
}

I am setting the html of an element from a post. Sometimes it doesnt load so when it doesnt load I would like for it to do something, is this possible. The way I have it now it always does the other thing because it hasnt set the html by the time it checks that it has html.

A: 

Hopefully somebody has a better solution than myself. A quick workaround is to use setTimeout() to delay your next calls, giving your HTML block enough time to load (since the load time is so miniscule).

$('#some_div').html('<div>some content</div>');
// set timeout for 250 milliseconds
setTimeout(callbackFunction, 250);

function callbackFunction() {
    // do some cool sh*t
}

Note, however, that if you wanted to pass any variables to your function, you could do the following:

$('#some_div').html('<div>some content</div>');
// set timeout for 250 milliseconds
var myParam = 'value';
setTimeout(function() { callbackFunction(myParam); }, 250);

function callbackFunction(myParam) {
    // do some cool sh*t
    alert(myParam);
}
cballou
That wouldnt really work because if the post takes longer than the timeout that I set it will fail, but thanks for the help.
ngreenwood6
You should be setting the html within the POST callback. An ajax request was not in your initial question.
cballou
+6  A: 

html() is a synchronous operation. The actual updating of the DOM depends on what your html content is. If you have <img> or <iframe> tags, they will take time to load. The next statement following the html() should immediately see the new html contents.

Did you mean load() instead?

[Edit] Based on your comment, it is probably your $.ajax failing. Not html(). Try attaching a failure handler to ajax call and retry from there? Although, if your server side code silently fails with a 200 code, the failure event doesn't get called. In that case, you can validate the html value before you set it to the element.

Chetan Sastry
+1. Judging by ngreenwood6's comment on his own question, the ajax call is the problem.
BipedalShark
there is no problem with the ajax call. it works perfectly fine I am watching it in firebug. just sometimes it completes and shows me the data in firebug but the data is not there. No I am no using load. I am posting to a php page which is then calling my database class that is then returning data from the database to the user.
ngreenwood6
BipedalShark I dont think you know what you are talking about...You were trying to get me to use an infinite loop and you removed your post.
ngreenwood6
In my defense, I did immediately state it was a cheap hack. I deleted it because the premise of your question is fishy. As Chetan Sastry points out, the html() method is synchronous. There's no need for a callback.
BipedalShark
as I said the ajax is not failing. I added an error event to it and it never executed. Also when I look at the response in firebug it has the information.
ngreenwood6