Last active
August 29, 2015 13:56
-
-
Save tomasgreen/9293238 to your computer and use it in GitHub Desktop.
Simple jquery script enabling flexible textarea height
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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