Last active
December 12, 2015 05:28
-
-
Save orioltf/4721831 to your computer and use it in GitHub Desktop.
#JQUERY: Autoresize textarea. The textarea will resize itself automatically as you add/remov content. For sure can be improved!
http://codepen.io/orioltf/pen/euImv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<textarea id="comment" rows="0" cols="50"></textarea> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
$.fn.autoResizeTextarea = function() { | |
return this.each(function() { | |
if(this.tagName.toLowerCase() != "textarea") return; | |
var rows = parseInt(this.getAttribute("rows")); | |
if(rows < 2) this.setAttribute("rows", 2); | |
else rows--; | |
$(this).on('keyup', function() { | |
var textLength = this.value.split("\n").length; | |
if(rows < textLength) { | |
console.log('1'); | |
this.setAttribute("rows", textLength+1); | |
} else { | |
console.log('2'); | |
this.setAttribute("rows", rows+1); | |
} | |
}); | |
}); | |
} | |
})(jQuery); | |
$('#comment').autoResizeTextarea(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://codepen.io/orioltf/pen/euImv