Skip to content

Instantly share code, notes, and snippets.

@vielhuber
Last active September 22, 2017 23:12
Show Gist options
  • Save vielhuber/e804d6239c3a95a2f509 to your computer and use it in GitHub Desktop.
Save vielhuber/e804d6239c3a95a2f509 to your computer and use it in GitHub Desktop.
textarea Maxlength crossbrowser #js
<span class="counter">150</span>
<textarea data-maxlength="150"></textarea>
$('textarea').bind('input propertychange', function() {
var limit = $(this).attr('data-maxlength');
var chars = $(this).val().length;
if(chars > limit){
$(this).val($(this).val().substring(0,limit));
chars = limit;
}
$('.counter').text(limit - chars);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment