Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic content doesn't work when loaded with ajax

I'm trying to load some dynamic content after the user has logged in, using $.ajax, like so:

$.ajax({
    url: "functions.php",
    type: "GET",
    data: login_info,
    datatype: 'html',
    async: false,
    success: function (response) {
        $('#main').html(response);
    }
});

The problem is, that some events don't work, when loaded this way. I fixed the buttons with the .live() method, but a sortable list for example, doesn't work. How can I somehow refresh the DOM, and let jquery know of these new added elements?

Thanks!

like image 931
Wurlitzer Avatar asked Dec 22 '25 07:12

Wurlitzer


2 Answers

There is no simple answer. I have just been coding a jQuery script and faced the same problem.

Just use this, i.e. the "Rebinding" solution

You would need to reinitialize UI components in $.ajax's success callback, e.g.:

$.ajax({
    url: "functions.php",
    type: "GET",
    data: login_info,
    datatype: 'html',
    async: false,
    success: function (response) {
        $('#main').html(response);
        $("#main .sortable").sortable();
    }
});
like image 41
karim79 Avatar answered Dec 23 '25 21:12

karim79