Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using JavaScript to change table cells width

My table td width in CSS is 150px.

I am thinking of using JavaScript to change the particular td width to 2px.

How can it be done?

like image 529
C PWL Avatar asked Nov 15 '11 02:11

C PWL


People also ask

How do you change cell width in a table?

To adjust column width automatically, click AutoFit Contents. To adjust table width automatically, click AutoFit Window.

How do you change the width of a table cell in HTML?

To set the cell width and height, use the CSS style. The height and width attribute of the <td> cell isn't supported in HTML5. Use the CSS property width and height to set the width and height of the cell respectively. Just keep in mind, the usage of style attribute overrides any style set globally.


1 Answers

Something like this (for the first one:

document.getElementsByTagName('td')[0].style.width = '2px';

Or this for all:

var tds = document.getElementsByTagName('td');

for (var i = 0; i < tds.length; i++)
    tds[i].style.width = '2px';
like image 78
qwertymk Avatar answered Oct 02 '22 23:10

qwertymk