Phillip Senn
Phillip Senn

Reputation: 47595

Table data width

I'm writing an editor, and would like for the input field to be the width of the column. So far, I have:

$td.html('<input name="test" value="' + $td.text() + '" size="' + $td.width() + '">');

The problem is: $td.width() is reporting numbers like 80 and 25, but the input needs to be in character widths. Even though my css says:

html {
    font-size:20px;
}

I don't see any relation between the $td.width() and the size attribute. You would think it would be $td.width()/20.

Upvotes: 0

Views: 254

Answers (2)

Mahesh
Mahesh

Reputation: 2256

From W3Schools http://www.w3schools.com/tags/att_input_size.asp

Since the size attribute is a visual design attribute, it is recommended to use CSS instead

$td.html('<input name="test" value="' + $td.text() + '" style="width: ' + $td.width() + '">');
// what does $td.width() return? with px? if not add 'px' or other required unit too.

Upvotes: 2

Gavin Coates
Gavin Coates

Reputation: 1425

I believe font-size:20px specifies the hight of the font, not the width. Width will be heavily dependent on font used, and will vary based on characters (unless fixed width). You will need to find out the default font used by a textbox.

I have however, found the following code snippet which claims to set the width of a textbox in pixels rather than characters:

<input type="text" name ="sizeGiven" style="width: 200px" />

Try entering width: 100% or similar and see if that works?

Upvotes: 1

Related Questions