views:

826

answers:

3

I have the following code:

//------------------------------------------------------//
// When the document is ready, start firing our AJAX    //
//------------------------------------------------------//
$(document).ready(function() {
    $("#navIndex a").click(function() {
        this.blur();
        return false;
    });
    $("#navPrevNext a").click(function() {
        this.blur();
        return false;
    });

    // Bind actions...
    $("#navIndex a").click(function(e) { e.preventDefault; updateNavigation($(this).attr('href')); });
    $("#navPrevNext a").click(function(e) { e.preventDefault; updateNavigation($(this).attr('href')); });
(); });
});

//--------------------------------------------------------------------------//
// METHODS - Get the params from the page and execute a server side call    //
//--------------------------------------------------------------------------//
function updateNavigation(pageIndex) {
    var filters = $("form").serialize();
    var productGroup = $("#valProductGroup").attr('title');
    var productType = $("#valProductType").attr('title');
    var itemsPerPage = $("#ItemsPerPage").val();
    var menuString = $("#Navigation").html();

    // repopulate the paging menu...
    $.ajax({ url: "/Catalog/Ajax/Update/Navigation"
          , type: 'GET'
          , dataType: 'HTML'
          , data: { productGroup: productGroup, productType: productType, itemsPerPage: itemsPerPage, pageIndex: pageIndex, filters: filters }
          , success: function(results) { $("#Navigation").html(results) }
          , failure: function(results) { $("#Navigation").html(oldMenuString) }
});

    // stop event bubbling... (this is not working as expected?)
    return false;
}

The page can be found at http://staging1.roomsalive.com/Catalog/Flooring/Hardwood. The page navigation (First/Prev/1/2/3/Next/Last) is what I am working on. When I first get to the page and click on "2" or "3" it behaves the way I expect. Once the menu is refreshed I then click on any of the other viable options, like "3", and it posts to http://staging1.roomsalive.com/Catalog/Flooring/Hardwood/3 instead of executing the JQuery call. I am 99% sure this is because when I load the document I attach the JQuery click events to the menu. Yet, when it posts back, those events are lost. How do I reattch them? Is that the problem?

TIA

A: 

Try

$("#navIndex a").click(function(e) { 
    e.preventDefault; 
    updateNavigation($(this).attr('href'));
    return false; 
});
$("#navPrevNext a").click(function(e) { 
    e.preventDefault; 
    updateNavigation($(this).attr('href'));
    return false; 
});
mattphp
A: 

It looks like when the page loads for the first time, the events get bound to the elements but when you delete those elements and replace them with similar ones... they are new elements that have no events bound.

you could modify the success and failure functions to bind the functions to the new elements after the DOM is changed by the $.html() call.

rennat
+3  A: 

I can think of two reasons for this, perhaps both need to be solved. If you are replacing the elements with the click handlers attached when you update the navigation you'll either need to reapply the handlers or use the live binding which automatically binds the handler to any element, current or future, that matches the selector.

$("#navIndex a").live('click', function() {
    this.blur();
    return false;
});

Second, while you are returning false in the method that calls the AJAX, you don't propagate that value back up the call chain. Make sure that your click function returns the result of the updateNavigation method. Or simply return false.

    $("#navIndex a").live('click', function(e) { e.preventDefault; return updateNavigation($(this).attr('href')); });
tvanfosson
Almost working. It is now passing the whole URL instead of just the pageIndex to my AJAX call. JQuery barfs the 2nd time around.Is there a way to inspect what is attached after the page is refreshed?
Keith Barrows
Look at http://exceptioneer.com/Share/Summary.aspx?abf65e20-4903-4540-af9c-e57634f5ca8e to see what is being passed on the 2nd time around (after 1st ajax call and repopulation of the navigation control)...
Keith Barrows
Use Firefox with the Web Developer plugin. Click on View Source, then View Generated Source to see the page source after the ajax call.
tvanfosson
If you're not going to use the live event handler you need to reattach the handlers in the ajax success callback not the method that invokes the ajax call. The method that invokes the ajax call will return before the ajax call completes and the new elements won't have been added to the DOM yet.
tvanfosson
I'm still not getting it to work. Also, using the Web Dev plugin for FF (View any of the options for surce) I am not seeing any events on anything. I expected to see something like <a href="..." onclick="...">
Keith Barrows
@Keith -- the text of the tag won't change when you add an onclick handler. What you should see is the code that sets up your event handlers. Have you tried using the live binding? When I last looked at your referenced code, you hadn't.
tvanfosson