Skip to content

Instantly share code, notes, and snippets.

@tomasgreen
Last active August 29, 2015 13:56
Show Gist options
  • Save tomasgreen/9293238 to your computer and use it in GitHub Desktop.
Save tomasgreen/9293238 to your computer and use it in GitHub Desktop.
Simple jquery script enabling flexible textarea height
(function ($) {
/* Simple jquery script enabling flexible textarea height. This seems to actually work and doens't require anything more than... this */
$.fn.flexHeight = function () {
var $obj = $(this);
if (!$obj.length) {
return;
}
if ($obj.attr('flexHeightInitialized')) {
$obj.trigger('propertychange');
return;
}
$obj.attr('flexHeightInitialized', true);
$(window).resize(function () {
$obj.trigger('propertychange');
});
setHeight($obj);
$obj.on('input propertychange keyup change', function () {
var $obj = $(this);
setHeight($obj);
});
};
var setHeight = function ($obj) {
$obj.each(function (index) {
var $i = $(this);
$i.css('height', 0);
var height = $i[0].scrollHeight;
height += parseInt($i.css('border-bottom-width').replace('px', ''));
height += parseInt($i.css('border-top-width').replace('px', ''));
$i.css('height', height);
});
};
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment