Created
July 14, 2011 16:42
-
-
Save jdlrobson/1082827 to your computer and use it in GitHub Desktop.
make a textarea resize as you type into it!
This file contains 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
// BSD License - written by jon robson (http://jonrobson.me.uk) | |
// see http://jon.tiddlyspace.com/jquery autoresize | |
function autoResize(el) { | |
var resize = function(ev) { | |
el = ev.target; | |
var div = $('<div />').addClass($(ev.target).attr("class")).hide(). | |
css({ "word-wrap": "break-word" }).appendTo($(el).parent())[0]; | |
var value = $(el).val() || ""; | |
var lines = value.split("\n"); | |
for(var i = 0; i < lines.length; i++) { | |
$("<span />").text(lines[i]).appendTo(div); | |
$("<br />").appendTo(div); | |
} | |
var h = $(div).height() + 50; | |
$(ev.target).height(h); | |
$(div).remove(); | |
}; | |
$(el).focus(resize).keyup(resize).blur(resize); | |
$(el).focus(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment