Skip to content

Instantly share code, notes, and snippets.

@cuing
Created December 11, 2013 02:55
Show Gist options
  • Save cuing/7904461 to your computer and use it in GitHub Desktop.
Save cuing/7904461 to your computer and use it in GitHub Desktop.
#autoresize {
resize: none;
outline: none;
width: 300px;
word-break: break-all;
font-family:sans-serif;
font-size: 1em;
line-height: 1.2em;
margin: 0px;
}
#hiddenDiv {
width: 300px;
word-break: break-all;
line-height: 1.2em;
min-height: 1.2em;
font-family:sans-serif;
font-size: 1em;
display: none;
}
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<meta charset=utf-8 />
<title>textarea auto height</title>
</head>
<body>
<textarea id="autoresize" rows="1"></textarea>
<div id="hiddenDiv"></div>
</body>
</html>
var obj = $('#autoresize');
var hidden = $('#hiddenDiv');
function autoHeight() {
console.log(obj.value);
hidden.style.height = obj.offsetHeight;
}
obj.keyup(function() {
var txt = obj.val();
hidden.text(txt);
obj.css('height', hidden.height());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment