Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add character count for inputs and textareas #2162

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions js/10-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,31 @@ Liberapay.init = function() {
$('button[data-action="reload"]').on('click', function() {
location.reload();
});

// Create dynamic hint based on remaining length
$('input[maxlength], textarea[maxlength]').on('focus input', function() {
if (!maxLength) {
var maxLength = $(this).attr('maxlength');
$(this).data('maxLength', maxLength);
$(this).removeAttr('maxLength');
}

var maxLength = $(this).data('maxLength');
var remainingLength = maxLength - $(this).val().length;
var helpBlock = $(this).siblings('.help-block');
Changaco marked this conversation as resolved.
Show resolved Hide resolved

if ($(this).val().length) {
helpBlock.text(function (index, value) {
return value.slice(value.indexOf('.') + 1);
Changaco marked this conversation as resolved.
Show resolved Hide resolved
});
helpBlock.prepend(remainingLength + ' characters remaing.');
Changaco marked this conversation as resolved.
Show resolved Hide resolved
} else {
helpBlock.text(function (index, value) {
return value.slice(value.indexOf('.') + 1);
});
helpBlock.prepend('Maximum length is ' + maxLength + '.');
}
});
};

$(function(){ Liberapay.init(); });
Expand Down