Skip to content

Instantly share code, notes, and snippets.

@jdlrobson
Created July 14, 2011 16:42
Show Gist options
  • Save jdlrobson/1082827 to your computer and use it in GitHub Desktop.
Save jdlrobson/1082827 to your computer and use it in GitHub Desktop.
make a textarea resize as you type into it!
// 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