0

After hours of trying to figure out where I went wrong, I finally found out that you cannot set the rows of the textarea once you set the scrollHeight for the textarea. Why is that happening, and how would we fix it?

<?php
include("ajaxLink.php");
?>

<textarea id = 'textarea' cols = '30' rows = '3' onkeydown = "changeIt()">
text text text text text text text text 
text text text text text text text text 
text text text text text text text text 
text text text text text text text text 
text text text text text text text text 
text text text text text text text text 
text text text text text text text text 
text text text text text text text text 
text text text text text text text text 
</textarea>

<script>

//******************************************************************

function changeIt() {

$('#textarea').height($('#textarea')[0].scrollHeight); 
document.getElementById('textarea').rows = 1000; 

} //end of function changeIt()
//******************************************************************

</script>

Obviously, this isn't the actual function that I'm using, and it's just here to serve as an example to prove that setting the rows of the textarea doesn't work after setting the scrollHeight.

jessica
  • 1,667
  • 1
  • 17
  • 35

1 Answers1

0

I am not sure if this will serve you purpose but what I understand from comments is you want to auto adjust the textarea height on keydown. But I am not sure what is the requirement on blur.

I assume it will go back to it original height

CSS

textarea {
    min-height: 52px;
}

JS

$("#textarea").on('keydown',function(){
        $(this).height( 0 );
        $(this).height( this.scrollHeight );
      })
    $("#textarea").on('blur',function(){
       $(this).height(0);
    })

jsfiddle demo

brk
  • 48,835
  • 10
  • 56
  • 78
  • I need both scrollHeight and setting the rows. they need to work in conjunction with each other. – jessica Feb 09 '16 at 04:35