0

I have set up a partial view to render in an index page. The partial view gets posted using Ajax to the server when a user clicks a sort button. This is so the entire page won't refresh just the partial view table.

The problem is after the first sort, the JavaScript in the index page is no longer effective. I worked around this by putting the js in the partial view itself to persist the events, but this produces js errors to saying 'continue' or 'ignore'.

1 Answer 1

2

It is because your newly injected elements ( via ajax) is not aware of the events bounded. So those event bindings are not availabel to them.

You should change your event bindings to use on so that it handles the current elements and future elements (added to DOM dynamically via ajax or so)

for example, if you want to handle the click event for elements with css class someCssClassSelector,

Change

$(".someCssClassSelector").click(function(){
  //do something
});

to

$(document).on("click",".someCssClassSelector",function(){
  //do something
});
Sign up to request clarification or add additional context in comments.

2 Comments

that works, what if I need to persist a 'scroll' event is that possible, that's the other feature that needs to persist. I will update the question.
@RayEatmon: Do not update the question. Post as a new question with relevant details

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.