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.
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.
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.
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.
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>
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); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With