57 lines
1.3 KiB
JavaScript
57 lines
1.3 KiB
JavaScript
jQuery.fn.limitMaxlength = function(options){
|
|
|
|
var settings = jQuery.extend({
|
|
attribute: "data_maxlength",
|
|
onLimit: function(){},
|
|
onEdit: function(){}
|
|
}, options);
|
|
|
|
// Event handler to limit the textarea
|
|
var onEdit = function(){
|
|
var textarea = jQuery(this);
|
|
var maxlength = parseInt(textarea.attr(settings.attribute));
|
|
|
|
if(textarea.val().length > maxlength){
|
|
textarea.val(textarea.val().substr(0, maxlength));
|
|
|
|
// Call the onlimit handler within the scope of the textarea
|
|
jQuery.proxy(settings.onLimit, this)();
|
|
}
|
|
|
|
// Call the onEdit handler within the scope of the textarea
|
|
jQuery.proxy(settings.onEdit, this)(maxlength - textarea.val().length);
|
|
}
|
|
|
|
this.each(onEdit);
|
|
|
|
return this.keyup(onEdit)
|
|
.keydown(onEdit)
|
|
.focus(onEdit)
|
|
.live('input paste', onEdit);
|
|
}
|
|
|
|
|
|
$(document).ready(function(){
|
|
|
|
var onEditCallback = function(remaining){
|
|
|
|
|
|
if(remaining == 0){
|
|
$(this).siblings('.charsRemaining').text("aucun caractère restant");
|
|
}else if(remaining == 1){
|
|
$(this).siblings('.charsRemaining').text(remaining+" caractère restant");
|
|
}else if(remaining > 1){
|
|
$(this).siblings('.charsRemaining').text(remaining+" caractères restants");
|
|
}
|
|
|
|
}
|
|
|
|
var onLimitCallback = function(){
|
|
|
|
}
|
|
|
|
$('.obs_textarea').limitMaxlength({
|
|
onEdit: onEditCallback,
|
|
onLimit: onLimitCallback
|
|
});
|
|
}); |