1

I have an HTML table. How can I change the width of one of the cells of that table from Javascript code? For example, I have a button, and when I press it, I want the width of one particular column to be changed. How can I do this?

5

3 Answers 3

8

You could do it without jQuery.

Working example: http://jsfiddle.net/qFDBk/1/

var btn = document.getElementById('btn');

btn.onclick = function() {
    var tbl = document.getElementById('tbl');
    var td = tbl.rows[0].cells[0];
    td.width = '500px';
    td.style.backgroundColor = 'blue';
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for giving an example that doesn't require jQuery
3

Here's how you might do it with jQuery, a popular JavaScript library:

http://jsfiddle.net/VJTnN/2

$('td:nth-child(2)').css('width', '100px');

Comments

1

Use CSS method to apply the width.

$('td').css('width','200px');

explanation :

  1. Get All td Elements. and Apply the CSS property width and set it's value to 200px. for every td element.
  2. $('td') is the tagSelector. it will select all td elements.
  3. .css() will apply the style rules which u mentioned.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.