Last active
May 31, 2016 11:34
-
-
Save kwarkjes/935c52813ef2305da8b2 to your computer and use it in GitHub Desktop.
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
// See demo: http://codepen.io/kwarkjes/pen/VjYYqG | |
$('[data-maxlength]').each(function() { | |
var $checker = $(this); | |
var data = $checker.data('maxlength'); | |
$(data.selector).keyup(function() { | |
var diff = parseInt(data.max - $(this).val().length); | |
$checker.text(diff).css('color', diff < 1 ? 'red' : ''); | |
}).keyup(); | |
}); | |
/* | |
HTML MARKUP | |
<textarea id="social-message">Hello World!</textarea> | |
<span data-maxlength='{"selector": "#social-message", "max": 140}'>140</span> of 140 characters | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment