3

I have some JS files included in my page that are simple for displaying blocks on click ant etc..

On another part of page, I have a button. When I click it an ajax call is made that returns some values that I display on the page. To display it, I'm reloading part of page like this:

$(document).ready(function () {
    $(document).on('click', '.add', function (e) {
    $this = $(this);
    $.ajax({
        type: 'POST',
        url: 'add',
        dataType: 'JSON',
        data: {product: $this.parent('.input-append').find('input').data('id'),quantity: $this.parent('.input-append').find('input').val()},
        success: function (data) {
          if(data.success == false){
           alert('error')
          }else{
            $('.test').load(" .test");
            $('.sidebar').load(" .sidebar");
            $('.top').load(" .top");
           }
        }
    });
});

This reloads part of page, displays values and etc..

However, after the ajax call is made, the JS stops working. When I click my buttons, nothing happens. No errors or anything.

I think it has to do with the ajax when I refresh part of twig and it messes up the previously loaded JS files. But what can I do in that situation? Somehow refresh the loaded JS files? How?

3
  • any event is not working for that specific area, which is return by ajax right? Commented Jun 3, 2016 at 11:54
  • You should have an init() function to reset all the js events handlers. And call the function after each ajax() success response. So: init() -> ajaxCall() -> init(); Because your dom is being changed after each ajax request. Commented Jun 3, 2016 at 11:55
  • jquery.load expects a url and you are giving class selector. This wont work, look for console, there might be some error. Commented Jun 3, 2016 at 11:59

2 Answers 2

4

You have to attach event listener on button starting from the container which doesn't get reloaded by Ajax request, like this:

//#mainCont is a container that doesn't get reloaded by Ajax

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

Comments

1

As said @Nacho M, you need to reinit listener from the loaded element, so you hsould have something like this :

function init() {
    $(document).on('click', '.yourclass', function (e) {
     //your content
    }

     // add every button who needs to be reloaded.
}

Init them on loading page first :

$("document").ready(function() {
init();
})

And on success of Ajax call :

$.ajax({
    type: 'POST',
    url: 'add',
    dataType: 'JSON',
    data: {product: $this.parent('.input-append').find('input').data('id'),quantity: $this.parent('.input-append').find('input').val()},
    success: function (data) {
      if(data.success == false){
       alert('error')
      }else{
        $('.test').load(" .test");
        $('.sidebar').load(" .sidebar");
        $('.top').load(" .top");
        init();
       }
    }
});

Comments

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.