<input id="post" type="text" style="width:400px;"/>
You know when you go to a site, start typing in a textbox, and all of a sudden, your keyboard stops typing stuff because you've reached the character limit?
How can I make that happen for this textbox for 140 chars?
function limitChars(textid, limit, infodiv) {
var text = $('#'+textid).val();
var textlength = text.length;
if(textlength > limit) {
$('#' + infodiv).html('You cannot write more then '+limit+' characters!');
$('#'+textid).val(text.substr(0,limit));
return false;
}
else {
$('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
return true;
}
}
// Bind the function to ready event of document.
$(function(){
$('#comment').keyup(function(){
limitChars('comment', 140, 'charlimitinfo');
})
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With