Last active
September 22, 2017 23:12
-
-
Save vielhuber/e804d6239c3a95a2f509 to your computer and use it in GitHub Desktop.
textarea Maxlength crossbrowser #js
This file contains hidden or 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
<span class="counter">150</span> | |
<textarea data-maxlength="150"></textarea> |
This file contains hidden or 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
$('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