Javascript callback not firing when AJAX operation complete

Posted by MisterJames on Stack Overflow See other posts from Stack Overflow or by MisterJames
Published on 2010-03-23T20:57:35Z Indexed on 2010/03/23 21:43 UTC
Read the original article Hit count: 330

Filed under:
|
|

Given the following code on an ASP.NET MVC View:

<% using (Ajax.BeginForm("AddCommunity", 
    new AjaxOptions { UpdateTargetId = "community-list", 
    OnSuccess = "BindCommunityHover" }))
   { %>
    Add Community: <input type="text" id="communityName" name="communityName" /> 
    <input type="submit" value="Add" />
<% } %>

And the following JavaScript method in the file:

function BindCommunityHover() {
    $(".community-item-li").hover(
            function () { $(this).addClass("communityHover"); },
            function () { $(this).removeClass("communityHover"); }
        );
};

Is there any reason why BindCommunityHover is not being called when the AJAX result comes back?

The community-list div is properly updated (the action returns a partial view). I have also tried setting OnComplete (instead of OnSuccess) to no avail.

The BindCommunityHover method is called in a $(function(){...}); block when the page first loads, and for all existing .community-item-li elements, it works.

The partial result from my controller replaces all items in that div with more of the same class. The OnSuccess method is supposed to fire after the document is updated.

Update: k...this gets weird. I added the following to the BindCommunityHover method:

alert($(".community-item-li").size());

I'm getting 240 in the alert when the page loads and when the callback fires. So, the callback IS firing, jQuery is matching the elements but not applying the styles...

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about jQuery