Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make a textarea grow with text to a max height?

Tags:

html

css

textarea

I'm working on a project where I have data in divs that can be various sizes. When a user clicks on one of the divs, it needs to turn into an editable textarea. The width is constant, but I'd like the height to start as the height of the original div and then grow to a max height before adding a scrollbar. Is that possible?

like image 687
Darkenwolf Avatar asked Apr 05 '13 22:04

Darkenwolf


People also ask

How do I expand textarea automatically?

It can be achieved by using JavaScript and jQuery. Method 1: Using JavaScript: To change the height, a new function is created which changes the style property of the textarea. The height of the textarea is first set to auto. This value makes the browser set the height of an element automatically.

How can I limit textarea height?

You can set the size of a text area using the cols and rows attributes. To limit the number of characters entered in a textarea, use the maxlength attribute. The value if the attribute is in number. Specifies that on page load the text area should automatically get focus.

What is the way to keep users from typing text into a large text area?

For input type="text" , we can use the size attribute to specify the visible size of the field, in characters. But we can also use the maxlength attribute to specify the maximum amount of characters that can be entered. Browsers generally enforce such a limit.

How do I change the height of a textarea in HTML?

The rows attribute specifies the visible height of a text area, in lines. Note: The size of a textarea can also be specified by the CSS height and width properties.


3 Answers

You can use contenteditable and let the user input in a straight div, here's a demo: http://jsfiddle.net/gD5jy/

HTML

<div contenteditable>
    type here...
</div>

CSS

div[contenteditable]{
    border: 1px solid black;
    max-height: 200px;
    overflow: auto;
}
like image 72
Sandro Paganotti Avatar answered Oct 19 '22 15:10

Sandro Paganotti


There's an excellent A List Apart article on this topic: Expanding Text Areas Made Elegant

like image 45
olivergeorge Avatar answered Oct 19 '22 13:10

olivergeorge


Here is the JavaScript function

// TextArea is the Id from your textarea element
// MaxHeight is the maximum height value
function textarea_height(TextArea, MaxHeight) {
    textarea = document.getElementById(TextArea);
    textareaRows = textarea.value.split("\n");
    if(textareaRows[0] != "undefined" && textareaRows.length < MaxHeight) counter = textareaRows.length;
    else if(textareaRows.length >= MaxHeight) counter = MaxHeight;
    else counter = 1;
    textarea.rows = counter; }

here is the css style

.textarea {
height: auto;
resize: none; }

and here is the HTML code

<textarea id="the_textarea" onchange="javascript:textarea_height(the_textarea, 15);" width="100%" height="auto" class="textarea"></textarea>

it works fine for me

like image 6
Alex Ruhl Avatar answered Oct 19 '22 13:10

Alex Ruhl