Skip to content

Instantly share code, notes, and snippets.

@christopherbauer
Last active August 29, 2015 14:21
Show Gist options
  • Save christopherbauer/5dd4c7bf3e806f489af4 to your computer and use it in GitHub Desktop.
Save christopherbauer/5dd4c7bf3e806f489af4 to your computer and use it in GitHub Desktop.
Expand Focused Textareas
$('table tr').on('focus', 'input,textarea', function(event) {
var $blurFrom = $(event.delegateTarget);
$blurFrom.css("background-color","yellow");
$blurFrom.find("textarea[cols]").each(function(i, element) {
expandHeight($(element));
});
});
$('table tr').on('focusout', 'input,textarea', function(event) {
var $blurFrom = $(event.delegateTarget);
$blurFrom.css("background-color","white");
$blurFrom.find("textarea[cols]").each(function(i, element) {
collapseHeight($(element));
});
});
function expandHeight($this){
var rows = ($this.val().length / ($this.prop("cols")-2) + 1);
$this.prop("rows",rows);
}
function collapseHeight($this){
$this.prop("rows", 1);
}
<script src="AutoVerticalGrowTextArea.js"></script>
<table>
<tr id>
<td><textarea cols="20" rows="1">Wow. Much vertical resize as you type. So highlight on focus. Such amaze.</textarea></td>
<td><textarea cols="20" rows="1"></textarea></td>
<td>Content 1</td>
<td>Content 2</td>
<td>Content 3</td>
</tr>
<tr>
<td><textarea cols="20" rows="1"></textarea></td>
<td><textarea cols="20" rows="1"></textarea></td>
<td>Content 1</td>
<td>Content 2</td>
<td>Content 3</td>
</tr>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment