Skip to content

Instantly share code, notes, and snippets.

@jsocol
Created October 25, 2012 14:31
Show Gist options
  • Select an option

  • Save jsocol/3952873 to your computer and use it in GitHub Desktop.

Select an option

Save jsocol/3952873 to your computer and use it in GitHub Desktop.
How I'd do this
var field = document.getElementById('id_needs_change_comment');
var maxlength = Number(field.getAttribute('data-maxlength'));
var remaining = document.getElementById('comment-remaining-characters');
var remainingText = remaining.textContent;
field.addEventListener('keyup', function() {
var remainingChars = maxlength - this.value.length;
remaining.textContent = remainingText.replace('%d', remainingChars);
}, true);
needs_change_comment = forms.CharField(
# ...
widget=forms.Textarea(attrs={'data-maxlength': 500}), # Preferably, don't hard code 500 but get it from the model.
# ...
<span id="comment-remaining-characters">{{ _('%d characters remaining') }}</span>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment