Search code examples
jquerydatatablewidthmanualdynamic-columns

Set JQuery DataTable Columns Width manually


I am using JQuery datatable latest plugin. I have issue with header and body columns are not aligned. What I am trying to do is I want to read each header column's width and corresponding body column's width and set the maximum of two to both th and td. But I am not able to read the width property of th or td. How can I do this?

I tried the following which returns blank

$("myTable thead tr th).each(function(){ var width = $(this).width});

Solution

  • You should use below, replace width with width(). $(this) should call function and not property.

    NOTE - you have missed clossing quotes for $("myTable thead tr th), correct it.

    $("myTable thead tr th").each(function(){ var width = $(this).width()});
    

    If you want to use property then use this instead of $(this)

    $("myTable thead tr th").each(function(){ var width = this.width});