Skip to content

Instantly share code, notes, and snippets.

@hadaytullah
Created October 25, 2019 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hadaytullah/3ff98e47c1879b4626be42baddabd61e to your computer and use it in GitHub Desktop.
Save hadaytullah/3ff98e47c1879b4626be42baddabd61e to your computer and use it in GitHub Desktop.
Auto extending HTML text area
$('#ta').on('keydown',function(){
var thisInput = event.target;
var $thisInput = $(thisInput);
var previousScrollHeight = $thisInput.data('previous-scroll-height');
if(previousScrollHeight === null || previousScrollHeight === undefined){ //first time
$thisInput.data('previous-scroll-height',thisInput.scrollHeight);
previousScrollHeight = thisInput.scrollHeight;
}
if(previousScrollHeight !== thisInput.scrollHeight){
$thisInput.css('height', 'auto' );
$thisInput.height( thisInput.scrollHeight );
$thisInput.data('previous-scroll-height', thisInput.scrollHeight);
}
});
<div id='canvas'>
</div>
<textarea id='ta'></textarea>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment