Muflix Muflix -4 years ago 250
Ajax Question

Ajax.BeginForm - How to access new DOM objects from PartialView?

I have following div, which is loading Partial View

<div id="_customerForm">
@Html.Partial("~/Views/Customer/_customerForm.cshtml", Model.customerForm)

In that Partial View I have Ajax Form

@using (Ajax.BeginForm("UpdateCustomer",
new AjaxOptions()
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "_customerForm",

And When I submit the Ajax Form, the Partial View, including Ajax Form, is regenerated (by new model from server)

The problem I am facing is that I have jquery event attached to one of the input of that form, which works, until the form is refreshed. For example

$("#CustomerAddressList").change(function () { ... });

Change event on
DOM works until
is refreshed by new PartialView model, any idea ?

Answer Source


$(document).on("change", "#CustomerAddressList", function(){


$("body").on("change", "#CustomerAddressList", function(){

Because the listener is bound to an element which is persistent on the page, it doesn't matter if the item which the selector references is dynamic content or not.

.on documentation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download