12

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?

3 Answers 3

16

Use jQuery's css method like this:

.css("width", width)

or this, if you plan on setting more attributes:

.css({"width":width})

Sign up to request clarification or add additional context in comments.

Comments

1

You could try :

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

Comments

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.