tags:

views:

45

answers:

3

I have an ASP .NET page where I use jQuery. I used this function to search an element in the DOM

$(document).ready(function() {

        currentID = $('#ctl00_ContentAreaPlaceHolder_hfCurrentID').val();
        if (currentID != "") {
            window.setTimeout(function() {
                $("div[class^='element'][ID='" +currentID + "']").trigger("click");
                $(window).scrollTop(500);
            }, 6000);
        } });

The problem is that when this code is executed al the DOM is not loaded, because I use an ajax call to create the grid that contains the 'element_r1c1' (r means row, and c means column) elements. Thats why I use the window.setTimeout to wait until the DOM is loaded, but this only works if the DOM is loaded in less than 6 seconds. So, I need a way to tell this function to execute after the ajax function has ended. For additional info, this code is in the page (.aspx) and the ajax function is in a control (.ascx)

Update:

I have two pages, the BasePage, the GridPage. The control is in the GridPage, and is called by the first page with

$('#tabs').tabs({

        select: function(event, ui) {
            // default shows All action elements
            $.ajax({
                url: 'GridPage.aspx?viewingDate=' + $(ui.tab).attr('Date'),
                cache: false,
                dataType: "html",
                success: function(data) {
                    //debugger;
                    $('#tabContent').empty().html(data);
                }
            });
            $('#nPaneArea').html('').removeClass().addClass('Pane');
        }
    });
+4  A: 

So you need to execute this script in the success callback of the AJAX script that is loading the grid. As you've tagged this with ASP.NET I guess you are using UpdatePanel to trigger AJAX calls. If this is the case you could use the endRequest event:

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function(sender, args) {
    // do the jQuery stuff here
});
Darin Dimitrov
The call is made with jQuery $.ajax
firematta
@firematta, well then it's even easier: simply use the `success` callback provided by jQuery.
Darin Dimitrov
A: 

As I understand, you're trying to execute a function after the DOM loads and you get a reply to an AJAX request.

You can set a flag in each completion handler for these two events, and, in each one, check whether the other flag is set, and, if it is, execute your function.

SLaks
could you explain me with code? Because I'm more like a backend guy, and I really got messy when I go to client side
firematta
A: 

Instead of waiting an arbitrary period of time and then executing unconditionally, check to see if you actually got anything with your jQuery selector every couple hundred milliseconds before acting.

function triggerClickOnReady(id) {
    return (function() {
        var $element = $("#" + id);
        if ($element.size() > 0) {
            $element.trigger("click");
            $(window).scrollTop(500);
        } else {
            window.setTimeout( triggerClickOnReady(id), 200);
        }
    });
}

You'll need to return a function so that you can pass in currentId and hold its value with a closure.

Then, in the success callback in your AJAX request...

success: function(data) {
    $('#tabContent').empty().html(data);
    triggerClickOnReady(currentId);
}
jasongetsdown
how to raise the event if I'm using $.ajax from jQuery to retrieve the grid?
firematta
Or is it possible to create a state machine in javascript?
firematta
If you're going to do it as a callback on the second example code you posted above it could be a little simpler. I'll edit my example...
jasongetsdown
Now that I think about it, you're selecting an element with a particular class, and then checking if it has a particular Id. Id's are unique, so all you need to do is check the Id. That will be a much faster selector. Editing again...
jasongetsdown
This is assuming `currentId` is available in the scope where this is called. I'm assuming it isn't known until runtime as you have it in your example.
jasongetsdown
Perfect, I'm going to implement it in my project
firematta