Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the number of rows in the textarea using jQuery

I have a textarea with 5 lines. I want to show only one line and on focus it should show remaining 4 lines.

like image 789
Josh R Avatar asked Dec 11 '10 14:12

Josh R


People also ask

How do I change the number of lines in a TextArea?

The variable lineHeight is set to the numeric value of the style property line-height of our TextArea . The variable tr represents the text in our TextArea and holds its dimensions as bounding properties. We divide the property boundingHeight by lineHeight to get the number of lines our text occupies.

How do I limit rows in TextArea?

I think you can do this with pure HTML. The wrap="hard" means that once the user has reached the end of the line - in this case, 20 characters - a newline will be inserted. Once the user has reached 100 characters - the same as filling in 5 lines with 20 characters - no more input will be allowed.


2 Answers

You can try something like this:

     $(document).ready(function(){      $('#moo').focus(function(){         $(this).attr('rows', '4');     }); }); 

where moo is your textarea.

like image 100
Vincent Ramdhanie Avatar answered Sep 22 '22 19:09

Vincent Ramdhanie


jQuery(function($){   $('#foo').focus(function(){     $(this).attr('rows',5);   }).blur(function(){     $(this).attr('rows',1);   }); }); 

Or, using less jQuery, less typing, and getting a hair more performance:

jQuery(function($){   $('#foo')     .focus(function(){ this.rows=5 })     .blur( function(){ this.rows=1 }); }); 
like image 34
Phrogz Avatar answered Sep 23 '22 19:09

Phrogz