Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save michaeljbailey/9c7e6f046bc4ccf17220 to your computer and use it in GitHub Desktop.
Save michaeljbailey/9c7e6f046bc4ccf17220 to your computer and use it in GitHub Desktop.
$('table tr').on('focus', 'textarea[rows][cols]', function(event) {
var $blurFrom = $(event.delegateTarget);
$blurFrom.css("background-color","yellow");
$blurFrom.find("textarea[rows][cols]").each(function(i, element) {
expandHeight($(element));
});
});
$('table tr').on('focusout', 'textarea[rows][cols]', function(event) {
var $blurFrom = $(event.delegateTarget);
$blurFrom.css("background-color","white");
$blurFrom.find("textarea[rows][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