Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery html callback

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.

like image 994
ngreenwood6 Avatar asked Nov 20 '09 23:11

ngreenwood6


People also ask

What is a callback in jQuery?

jQuery Callback Functions JavaScript statements are executed line by line. However, with effects, the next line of code can be run even though the effect is not finished. This can create errors. To prevent this, you can create a callback function. A callback function is executed after the current effect is finished.

What is .HTML in jQuery?

The html() Method in jQuery is used to set or return the innerHTML content of the selected element. Syntax: It returns the content of first matched element. $(selector).html() It sets the content of matched element.

Where do I put jQuery in HTML?

Step 1: Open the HTML file in which you want to add your jQuery with the help of CDN. Step 2: Add <script> tag between the head tag and the title tag which will specify the src attribute for adding your jQuery. Step 3: After that, you have to add the following path in the src attribute of the script tag.

How do I get inner text in jQuery?

Answer: Use the jQuery text() method You can simply use the jQuery text() method to get all the text content inside an element. The text() method also return the text content of child elements.


2 Answers

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.

like image 175
Chetan S Avatar answered Sep 20 '22 07:09

Chetan S


Chetan Sastry's answer(the highest answer now) is not 100% correct.

$('#some_div').html('<div id="new_div">some content</div>'); // code 1    
$('#new_div').click(function(){ }); //code 2

I don't know why, but in some cases, code 2 will be executed before code 1 finishing. There is a short time less than 0 millisecond between code 1 and code 2. It should be some special executing orders in Javascript.

You have to use cballou's way(the floor answer =_=!) to solve the problem. e.g.

var sync = function(fn, millisec){
    var m = millisec ? millisec : 0; //in face, 0 is enough
    return setTimeout(fn,m);
};

$('#some_div').html('<div id="new_div">some content</div>'); // code 1    
var code2 = function(){
    $('#new_div').click(function(){ }); // run code 2, here
};

sync(code2);
like image 45
Didikeke Avatar answered Sep 20 '22 07:09

Didikeke