Tuesday, March 25, 2014

JavaScript Function to manage AutoGrow in HTML TextArea

I've found this very simple and usefull jscript you can use to AutoGrow based on user input.


Add this script to head of your page (no jquery is required - just simple javascript).


<script type="text/javascript" language="javascript">
function resizeTextArea(ta) {
  cr = ta.value.split('\n');
  b = 1;
  for (x=0;x < cr.length; x++) {
   if (cr[x].length >= t.cols)
    b+= Math.floor(cr[x].length/t.cols);
  }
  b+= cr.length;
  if (b > ta.rows) ta.rows = b;
 }

</script>


Then add the handler for onKeyUp event on your textarea in this way.


onkeyup="resizeTextArea(this);"

No comments: