Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript code to resize textarea with animation effect on focus gain

I am working with web application and I want to resize textarea with animation effect (smoothly resize) when the textarea gain focus.

I tried following code to resize textarea on focus gain but it does not smoothly resize.

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <script type='text/javascript'>
         function abc()
         {
           $('#txt').attr('rows',15);
         }
    </script>
</head>
<body>

<textarea id='txt' rows="4" onfocus='abc();' cols="50">
this is testing of textrea
</textarea>

</body>
</html>
like image 674
Atish Bundhe Avatar asked Dec 11 '22 10:12

Atish Bundhe


2 Answers

If you dont need support for IE9 and older versions, pure CSS can solve your issue.

#txt {
    height:80px;
    transition: all 2s;
    -webkit-transition: all 2s; /* Safari */
}
#txt:focus {
    height:300px;
}

http://jsfiddle.net/MHC8T/

like image 53
Giovanni Silveira Avatar answered Apr 09 '23 18:04

Giovanni Silveira


try this:

function abc()
{
       $('#txt').animate({'height':"+=100px"}, 400);
}

you can switch the height to whatever you want.. the +=100 is relative and will add 100px to the height of the textarea

also as an external event handler

$("#txt").bind("focusin",function abc(){
       $('#txt').animate({'height':"+=100px"}, 400);
});

hope that helps

like image 40
Jonathan Marzullo Avatar answered Apr 09 '23 18:04

Jonathan Marzullo