Download jQuery and edit downloaded content

I use jquery loading, and then, as soon as the content is loaded into a div, I want to change some tags to language-dependent variables.

My problem is that I need to use setimeout to make the script wait long enough for the elements to be ready for editing.

When I use the callback function parameter, the elements that I want to edit are apparently not ready because they are not set. I hate using setimeout because it limits everyone to the slowest setting, and invariably some connections will be even slower than that.

Apparently, the callback method means that the ajax method got the html back, but it does not guarantee that the imported elements are really ready in dom.

Does anyone have any ideas?

current code

$("#content-basket").load("/BasketPage.htm?time=" + now.getMilliseconds()); ... ... setTimeout("timedbasket();", 500); ... ... function timedbasket() { alert($('#basketlegend')); $('#basketlegend').html(basketlabel); } 

I would like to be able to use

 $("#content-basket").load("/BasketPage.htm?time=" + now.getMilliseconds(), "", timedbasket()); 

Here is the source of basket.htm

 <tr> <td> <div id="basket"> <fieldset> <legend><span id="basketlegend"></span></legend> <table id="baskettbl" border="0" class="basket-table"> <tbody> <tr class='total'> <td> <span id="empty"></span> </td> </tr> </tbody> </table> </fieldset> </div> </td> </tr> 
+4
source share
2 answers

Use the callback parameter to load and put your code in the callback.

 $("#content-basket").load("/BasketPage.htm?time=" + now.getMilliseconds(),null, function() { ...do stuff here after #content-basket is finished loading... }); 

After looking at jQuery, it looks like the answer is entered before calling the callbacks, so the DOM needs to be updated. Is it possible that you are reusing jQuery objects that can refer to DOM elements that were previously there but were replaced by a load?

+2
source

jQuery.live is the answer. Bind your events to DOM elements in this way, and even new ones will be affected.

0
source

All Articles