Created
October 25, 2012 14:31
-
-
Save jsocol/3952873 to your computer and use it in GitHub Desktop.
How I'd do this
This file contains hidden or 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
| 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); |
This file contains hidden or 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
| needs_change_comment = forms.CharField( | |
| # ... | |
| widget=forms.Textarea(attrs={'data-maxlength': 500}), # Preferably, don't hard code 500 but get it from the model. | |
| # ... |
This file contains hidden or 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
| <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