Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to expand a text area when click on

Tags:

javascript

css

Im working on a small project which has a textarea and i need help in making the text area expand on mouse click like that of twitter and facebook. the textarea should look like a textfield at first then when clicked on should expand.

like image 807
Uchenna Avatar asked Apr 13 '11 11:04

Uchenna


People also ask

How do I fix the size of the text area?

This simple CSS code disables resizing of the element. Now you can use height and width property to provide a fixed height and width to the element. Some developers also use cols and rows css property to provide textarea size.

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 add text area?

To add text to a textarea, access the value property on the element and set it to its current value plus the text to be appended, e.g. textarea. value += 'Appended text' . The value property can be used to get and set the content of a textarea element.


2 Answers

This can be done without the use of JavaScript/jQuery, using CSS transitions.

textarea {      height: 1em;      width: 50%;      padding: 3px;      transition: all 0.5s ease;  }    textarea:focus {      height: 4em;  }
<textarea rows="1" cols="10"></textarea>
like image 147
Ben Fortune Avatar answered Oct 12 '22 01:10

Ben Fortune


Something like this would work...

Demo: http://jsfiddle.net/Y3rMM/

CSS...

.expand {     height: 1em;     width: 50%;     padding: 3px; } 

HTML...

<textarea class="expand" rows="1" cols="10"></textarea> 

jQuery...

$('textarea.expand').focus(function () {     $(this).animate({ height: "4em" }, 500); }); 
like image 23
wdm Avatar answered Oct 12 '22 02:10

wdm