Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't control the width of a table-cell (<code>td</code>) unless you wrap its contents inside a container control like <code>div</code>. The following jQuery function will wrap the contents of each td inside a div.</p> <pre><code>function WrapTableCellsWithDiv(tableId) { $('#' + tableId + ' tbody tr td').each(function () { //get corresponding th of this td var tdIndex = $(this).index(); var th = $('#' + tableId + ' thead tr th:nth-child(' + (tdIndex + 1) + ')'); var thWidth = $(th).width(); //wrap the contents of td inside a div var tdContents = $(this).html(); var divTag = '&lt;div style="width: ' + thWidth + '"&gt;' + tdContents + '&lt;/div&gt;'; $(this).html(divTag); }); } </code></pre> <p>This function will wrap the contents of each <code>td</code> element inside a <code>div</code> tag. The width of the <code>div</code> tag will be set to width of the corresponding <code>th</code> element.</p> <p>Sample table structure:</p> <pre><code>&lt;table id="SampleTable"&gt; &lt;thead&gt; &lt;tr&gt; &lt;th style="width: 90px;"&gt;FirstName&lt;/th&gt; &lt;th style="width: 90px;"&gt;LastName&lt;/th&gt; &lt;th style="width: 60px;"&gt;Age&lt;/th&gt; &lt;th style="width: 70px;"&gt;Gender&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;John&lt;/td&gt; &lt;td&gt;Smith&lt;/td&gt; &lt;td&gt;25&lt;/td&gt; &lt;td&gt;Male&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Ted&lt;/td&gt; &lt;td&gt;Eddie&lt;/td&gt; &lt;td&gt;22&lt;/td&gt; &lt;td&gt;Male&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Angel&lt;/td&gt; &lt;td&gt;Mike&lt;/td&gt; &lt;td&gt;23&lt;/td&gt; &lt;td&gt;Female&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload