ndesign11
ndesign11

Reputation: 1779

Jquery On() not working as expected

I am trying to use this row highlight function and it works when the page is loaded but when I use the pagination the function doesn't seem to stay on. The row no longer will highlight.

$("#json-table tbody tr").on("click", function(event){
    $(this).toggleClass('row_selected');
});

Upvotes: 1

Views: 86

Answers (2)

Tom Walters
Tom Walters

Reputation: 15616

Try:

$("#json-table").on("click", "tbody tr", function(){
    $(this).toggleClass('row_selected');
});

Because you reload the table, you have to bind the on() handler to something that won't be replaced, so use either #json-table if you only alter the children, or its parent if not.

Upvotes: 3

Barlas Apaydin
Barlas Apaydin

Reputation: 7315

You are not using correct syntax for on(), use like this:

$(document).on('click','#json-table tbody tr', function(event){
    $(this).toggleClass('row_selected');
});

Upvotes: 1

Related Questions