Created
April 9, 2014 07:21
-
-
Save Takaya213/f6147596a4582b7389c0 to your computer and use it in GitHub Desktop.
Character Limiter
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($) { | |
$.fn.extend( { | |
limiter: function(limit, elem) { | |
$(this).on("keyup focus", function() { | |
setCount(this, elem); | |
}); | |
function setCount(src, elem) { | |
var chars = src.value.length; | |
if (chars > limit) { | |
src.value = src.value.substr(0, limit); | |
chars = limit; | |
} | |
elem.html( limit - chars ); | |
} | |
setCount($(this)[0], elem); | |
} | |
}); | |
})(jQuery); | |
var elem = $("#msg_counter"); // Display span/div ID/Class | |
$("#message").limiter(250, elem); // Textarea ID |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment