Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
addClass if limit exceeded
- Loading branch information
Showing
2 changed files
with
37 additions
and
21 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,33 @@ | ||
(function($){ | ||
$.fn.counter = function() { | ||
return this.each(function() { | ||
max_length = parseInt($(this).attr('data-max-length')); | ||
(function ($) { | ||
$.fn.counter = function (options) { | ||
var defaults={ | ||
limitExceedClass:'' | ||
}; | ||
var options=$.extend({},defaults,options); | ||
return this.each(function () { | ||
max_length = parseInt($(this).attr('data-max-length')); | ||
|
||
var length = $(this).val().length; | ||
$(this).parent().find('.counter_label').html(max_length-length + ' characters left'); | ||
// bind on key up event | ||
$(this).keyup(function(){ | ||
// calc length and truncate if needed | ||
var new_length = $(this).val().length; | ||
if (new_length > max_length-1) { | ||
$(this).val($(this).val().substring(0, options.max_length)); | ||
} | ||
// update visual counter | ||
$(this).parent().find('.counter_label').html(max_length-new_length + ' characters left'); | ||
}); | ||
}); | ||
}; | ||
var length = $(this).val().length; | ||
$(this).parent().find('.counter_label').html(max_length - length + ' characters left'); | ||
// bind on key up event | ||
$(this).keyup(function () { | ||
// calc length and truncate if needed | ||
var new_length = $(this).val().length; | ||
if (new_length > max_length - 1) { | ||
$(this).parent().find('.counter_label').addClass(options.limitExceedClass); | ||
} | ||
else { | ||
$(this).parent().find('.counter_label').removeClass(options.limitExceedClass); | ||
} | ||
// update visual counter | ||
$(this).parent().find('.counter_label').html(max_length - new_length + ' characters left'); | ||
}); | ||
}); | ||
}; | ||
})(jQuery); | ||
$(document).ready(function () { | ||
$('textarea').counter(); | ||
|
||
}) | ||
|
||
|
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