You Are Here: Home » How-To » Programming

jQuery HTML() Callback Function Using Promise()

By on August 3rd, 2013 
Advertisement

jQuery has a cool function called the html() which basically replicates the functionality of the innerHTML method in Javascript. Using HTML() with a jQuery object you can replace the contents of any container element like the <div> or <p>, <span> and others.

However, like other functions in jQuery, the html() function does not have a callback function. Which means you are unable to check or get a notification when the html() function has finished its work. This is where the promise() and the done() functions of jQuery come into picture.

You can implement a callback kind of functionality with the html() function in the following way:

    $('#divId').html(someText).promise().done(function(){
        //your callback logic / code here
    });

The promise() function used above basically returns an object when certain watch actions which might or might not have been queued get finished. jQuery .html is a good function and convenient at times however, I would recommend to use the native javascript innerHTML method in order to achieve whatever you are trying to do with jquery .html as the native innerhtml is faster! Please let me know if you found this tip helpful.

Advertisement



Related Posts by Tags: , , , ,





jQuery HTML() Callback Function Using Promise() was originally published on Digitizor.com on August 3, 2013 - 5:28 pm (Indian Standard Time)