tags:

views:

157

answers:

2

Hi,

I am using Jquery load and then as soon as the content is loaded into the div I want to change some of the tags to language dependent variables.

My problem is that I am having to use a settimeout to get the script to wait long enough for the elements to be ready to edit.

When I use the callback function parameter the elements I want to edit apparently aren't ready because they don't get set. I hate to use settimeout because this limits everyone to the slowest setting and invariably some connections will be even slower than that.

Apparently the callback method just means that the ajax method got the html back but it doesn't ensure that the imported elements are actually ready in the dom.

Anyone have 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 basket.htm source

<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>
+1  A: 

Use the callback parameter for 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 sure seems like the response is injected before any callbacks are called so the DOM should be updated. Is it possible that you are are re-using jQuery objects that may reference DOM elements that were previously there, but were replaced by the load?

tvanfosson
I have tried the callback method but the imported elements don't get set when using the callback method, they aren't fully loaded in the dom yet apparently.
MvcCmsJon
Did you see my update?
tvanfosson
Yes, I copied your code and the basket label still does not get set. I thought the callback method meant the dom was ready but apparently it doesn't.
MvcCmsJon
Actually, I think you may have a different problem. I just looked at the jQuery source and it injects the HTML before calling any callback.
tvanfosson
No, the basket doesn't exist until the load happens. What has me stumped is that it works fine when timed. I will edit my post with the basket.htm source.
MvcCmsJon
Load uses ajax internally and does inject the HTML into the DOM before calling any callbacks. Look at the jQuery source.
tvanfosson
A: 

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

idrumgood
This would work of live accepted the onload event of basketlegend.Possible event values: click, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, keydown, keypress, keyupCurrently not supported: blur, focus, mouseenter, mouseleave, change, submitNice new feature of jquery though.
MvcCmsJon