AP257
AP257

Reputation: 93823

How to set the width of a textarea in jQuery?

I want to set the width of a textarea to match the width of a particular image. Using .width() works for setting the width of an image, but not of a textarea.

$(document).ready(function() {
    var width = $("#my_image").width();
    $("#another_image").width(width); // works
    $("#my_textarea").width(width); // fails
});

How do I set the width of a textarea?

Upvotes: 12

Views: 11211

Answers (3)

jondavidjohn
jondavidjohn

Reputation: 62392

$("#my_textarea").css('width',width);

Also you might want to use .outerWidth() to set var width depending on your padding/margin/border settings

Upvotes: 1

Keith
Keith

Reputation: 5381

Use jQuery's css method like this:

.css("width", width)

or this, if you plan on setting more attributes:

.css({"width":width})

Upvotes: 16

Rion Williams
Rion Williams

Reputation: 76557

You could try :

$("#my_textarea").css('width', width);

Upvotes: 1

Related Questions