Skip to content

Instantly share code, notes, and snippets.

@LowerDeez
Last active December 6, 2019 12:32
Show Gist options
  • Save LowerDeez/13ef77d606b0fc5d98a92544d756fbc1 to your computer and use it in GitHub Desktop.
Save LowerDeez/13ef77d606b0fc5d98a92544d756fbc1 to your computer and use it in GitHub Desktop.
Django. JS in admin panel for checking letters count.
class SomeModelAdmin(admin.ModelAdmin):
class Media:
js = ['path/to/script.js']
/**
* Created by Developer on 15.09.2017.
*/
(function ($) {
$(document).ready(function ($) {
function chars_check_count(text_field) {
text_field.keyup(function (event) {
var max_length = $(this).attr('maxlength');
var charsCurrent = $(this).val().length;
var spanChars = $(this).next('.help');
if (charsCurrent >= max_length){
spanChars.css('color', 'red').text(charsCurrent + '/' + max_length)
.append("<span> characters</span>");
}
else
{
spanChars.css('color', 'green').text(charsCurrent + '/' + max_length)
.append("<span> characters</span>");
}
})
}
$('input[type=text]').each(function(){
chars_check_count($(this))
});
});
})(django.jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment