Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resize textbox automatically according to the content

http://jsfiddle.net/h2vMN/1/

I have a text box text inside it already, in the actual application this will be filled dynamically, but for the sake of this question it has been pre filled.

<textarea id="textarea">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempor commodo ornare. Donec lobortis dui sed lectus egestas tristique. Vivamus vel metus turpis, faucibus varius risus. Aenean ante mauris, ultrices id facilisis vitae, dignissim eget sem. Quisque sed justo lectus, eget aliquet leo. Curabitur in mauris et diam fermentum venenatis. Proin ullamcorper, neque a vehicula euismod, odio enim aliquam ipsum, eu tristique urna sapien nec erat.

Aliquam erat volutpat. In in lacus cursus dolor pellentesque posuere. Cras eu metus urna, a rhoncus ligula. Ut hendrerit orci in arcu dignissim id fermentum orci vulputate. Sed ante ligula, volutpat eu convallis vel, auctor in metus. Mauris euismod, metus eget venenatis sodales, risus tellus volutpat elit, et aliquet massa tellus ut sapien. Mauris tempor posuere massa et consectetur. Duis dignissim enim a nulla ultricies vitae vulputate felis commodo. Phasellus mollis est eget odio porttitor consequat. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Phasellus ut nibh auctor libero sagittis semper vitae quis erat.
</textarea>​

When I run the above code, it shows a tiny text area with scroll bars all over it. In other words, completely useless in terms of being user friendly. How do I automatically resize the text box according to the amount of content their is and it has a set width of 600px.

#textarea{width:600px;}​

I would like a javascript/jquery solution.

I have tried the .autoresize solution unsuccessfully, which can be found here:

http://james.padolsey.com/demos/plugins/jQuery/autoresize.jquery.js/view

Note, the height should be resized automatically

like image 783
RSM Avatar asked Oct 22 '22 19:10

RSM


1 Answers

Thy this:

$(document).ready(function(){
    tx = $('#textarea')
    tx.height(tx.prop('scrollHeight'));
})

DEMO

like image 93
Sergei Zahharenko Avatar answered Nov 03 '22 18:11

Sergei Zahharenko