Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell when changes to jquery html() have finished?

I'm using jQuery to change the HTML of a tag, and the new HTML can be a very long string.

$("#divToChange").html(newHTML);

I then want to select elements created in the new HTML, but if I put the code immediately following the above line it seems to create a race condition with a long string where the changes that html() is making may not necessarily be finished rendering. In that case, trying to select the new elements won't always work.

What I want to know is, is there an event fired or some other way of being notified when changes to html() have finished rendering ? I came across the jQuery watch plugin, which works alright as workaround but it's not ideal. Is there a better way ?

like image 679
Michael Low Avatar asked Apr 03 '10 02:04

Michael Low


1 Answers

use .ready jQuery function

$("#divToChange").html(newHTML).ready(function () {
                    // run when page is rendered
                    });
like image 142
miralong Avatar answered Oct 31 '22 16:10

miralong