0

After I click on a th with the class database-header, my table gets sorted via the post method, but for some reason the click event listener gets removed, so when I click again on a different database-header, nothing happens. What em i doing wrong ?

$(function () {
    var user = $('#user');
    var databaseHeader = $('.database-header');
    var tableContainer = $('.table-container');
    var databaseHeaderValue;
    var url = './php/table.php';

    databaseHeader.click(function () { 
/* Checks for the user login div */
        if (user.closest('html').length){ 
            databaseHeaderValue = $(this).html();
            $.post(url, {'sort' : databaseHeaderValue}, function (data) {
                tableContainer.html(data);
            });
        }
    });
});
Mahir
  • 29
  • 4

2 Answers2

1

Try changing your jquery click like below:

var user = $('#user');
var databaseHeader = '.database-header';
var tableContainer = $('.table-container');
var databaseHeaderValue;
var url = './php/table.php';

$(document).on('click', databaseHeader, function () { 
    /* Checks for the user login div */
    if (user.closest('html').length){ 
        databaseHeaderValue = $(this).html();
        $.post(url, {'sort' : databaseHeaderValue}, function (data) {
            tableContainer.html(data);
        });
    }
});
Mahesh Singh Chouhan
  • 2,558
  • 1
  • 16
  • 26
0

Do you only refresh a part of the page? After a DOM element is added dynamically, you cannot grab it with a jquery selector.

try

$(body).on("click", ".database-header",function(){
etc..
}
Milney
  • 6,253
  • 2
  • 19
  • 33