Proton
Proton

Reputation: 236

ASP.NET MVC 2 Jquery event not fired

i'm building a view for editing a variable length list using this http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/ solution.
Here's my ActionLink:

 <%= Html.ActionLink("Add another...", "AddReceiver", null, new { id = "addItem" })%>

And here's my javascript code:

$("#addItem").click(function () {
$.ajax({
    url: this.href,
    cache: false,
    success: function (html) { $("#editorRows").append(html); }
});
return false;});

The problem is that click event is not fired, so i'm getting not the ajaxly updated view, but an empty page with my partial view, gotten from the AddReceiver action.
Any suggestions, guys?

Upvotes: 1

Views: 327

Answers (2)

Jason
Jason

Reputation: 15931

Is the click handler function defined in the $(document).ready (...) Function?

Upvotes: 0

David Glenn
David Glenn

Reputation: 24522

It sounds like your browser is following the link immediately after executing the click function and thus overriding your script. Try using the preventDefault() method of the event.

$("#addItem").click(function (event) {
  event.preventDefault();
  $.ajax({
    url: this.href,
    cache: false,
    success: function (html) { $("#editorRows").append(html); }
  });
  return false;
});

Upvotes: 2

Related Questions