I just tried changing my page contents with jQuery’s .load methdod.
While the contents changed without problem, I found that jQuery was still “seeing” the old content if I tried to select something, an example:
page:
<div id="mycontentarea">
<div id="myfirstcontent"></div>
<div id="mysecondcontent"></div>
</div>
replacing content:
<div id=mythirdcontent"></div>
<div id=myfourthcontent"></div>
javascript:
// replace original content
$('#mycontentarea').load('replacement.html');
// print out the id of the first child
console.log($("#mycontentarea").children().attr("id"));
The console will print out “myfirstcontent” instead of “mythirdcontent” – why?
Because
loadis asynchronous and yourconsole.logcall is executed before the replacement happens.Move any code that relies on the result of the call to
loadto a callback which is executed upon successful completion:From the docs on
load: