• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

Expanding textarea on key down


Ive got a situation whereby i need to be able to print the contents of a form out. The problem arises that there are textarea fields on the form. If the user has input so much information that it is only all viewable by scrolling to the bottom of the textarea then this part would not show up when printed.

The only way i can see round this is when a key is pressed within the textarea element, javascript needs to check whether there are more rows of text than the default value of rows....if so then the script needs to add another row each time this occurs.

Does anyone know how this can be done as i cant even find a script that detects how many rows a textarea element has???

Thanks in advance

Al Higgs
  • 2
1 Solution
the above works on keyup, you could always change it to keydown if you so choose.
Here is my try of the keyup event, it can be off any event eg onblur or form submit. The area expands when text is added and will contract if text is removed

function resize(obj){
if (numRows > minRows)
 return numRows
  else return minRows

<textarea rows=5 cols=20 onkeyup='this.rows=resize(this)'>

here with one line:
<textarea style=width=300px;height:23px onkeyup="this.style.height=this.scrollHeight+5"></textarea>
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now