In a plugin I’m writing I’m having terrible trouble with widths of table cells. All I do is get the width of the table cell using jQuery’s .width() function, then set that same cell to be the width returned. Basically, this:
$table.find('> thead > tr > th').each( function() {
var $th = $(this);
$th.css({width: $th.width()});
} );
However, in many instances the width returned is incorrect and setting it changes the width of the cells. At first it just seemed like it was off by 1px so I added 1px to the returned width. But with thicker borders it’s off by more – however it doesn’t seem to be a case of simply adding the border width. For starters, there are obviously 2 borders but it’s only off by the width of 1 border. And with varying border widths it seems almost random which value you need to add.
Here’s an example with my code – the width-setting part runs 1 second after page load so you can see the change. Is there a reliable way to get/set the width of table cells in Javascript?
this is your plugin almost rewrited… tested on IE7-10, Chrome, Firefox
css inside demo source!