Pages

Sunday, 13 October 2013

JQuery Textarea Expanding on Keypress

This lesson about to how the facebook wall status box expanding while pressing Enter and collapsing while pressing Backspace keyword. we can implement this kind of expanding's using JQuery and some logic to use .height() property.
let's see how will do this.

Javascript

here i am using keyup event function to the textarea .status class. now when we press enter key the actual height of the textarea box will increase the height +20 pixels and it will continues increase.
when we press backspace key the height of the textbox will decrease the present height to -20 pixels.

2 comments
:

Why does the "Post" button appear when you focus on the text area the first time but not disappear when you shift focus out of the text area?

Why do you keep switching between $(this) and $('.status') to refer to the same thing within the context of the "keyup" event handler (and I think you're missing the "event" parameter in that handler function)?

I made some changes to streamline and improve both the performance (in minor ways) and the understandability of the code: http://jsbin.com/bolaje/1/edit

Just in quick, rudimentary testing I found the following behavioral issues:

1. The line height of 20 doesn't really match up with the line height of the text in the text area so as you hit "Enter" over and over again the amount of white space between the last line of text and the bottom edge of the text area continues to increase.

1. The "Backspace" height reduction functionality doesn't quite work as-is since it doesn't account for someone holding down the backspace key or selecting multiple lines of text before hitting the backspace key.

I haven't put much thought into possible solutions for these issues. For the "holding down the backspace key" issue you might want to consider using a debounced function for the event handler. For the "line heights don't match up" issue you may want to consider grabbing the line height of the text area to use as the value instead of an arbitrarily chosen hard-coded value of 20.