2

the javascript below are tested in codepen but when it is not working in my webpage. the function supposed to freeze the first column of my table, the header remain static when I scroll my table to the left. after some discussion in the comments below it could be due to the html table is dynamically created, I am looking for help to convert the js below to handle this case.

$(document).ready(function() {
  $('.main-table tbody').scroll(function(e) {
    $('.main-table thead').css("left", -$(".main-table tbody").scrollLeft());
    $('.main-table thead tr th:nth-child(1)').css("left", $(".main-table tbody").scrollLeft()-2);
    $('.main-table tbody tr td:nth-child(1)').css("left", $(".main-table tbody").scrollLeft()-2);
  });
});

a few attempts to make it work for dynamically created table but still not working. can someone help to point out what i did wrong here:

$(document).on('scroll', '.main-table tbody', function(){
    $('thead').css("left", -$("tbody").scrollLeft());
    $('thead tr th:first-child').css("left", $("tbody").scrollLeft()-2);
    $('tbody tr td:first-child').css("left", $("tbody").scrollLeft()-2);
});
user466130
  • 299
  • 5
  • 17

0 Answers0