I use the jQuery function to generate a countdown of characters below the textarea field for our site users (the remaining X characters). My form lasts a few pages (somewhat reminiscent of a wizard), and users usually go back and forth across all pages. When I go back one step and reloading a page that has already been completed, my character countdown will display "3000 remaining characters." It does not consider the number of characters when loading the page. If the user returns through forms, I would like the function to be launched and count the number of characters that have already been entered in the textarea field.
# The jQuery Function
$(document).ready(function () {
$('textarea').on("load propertychange keyup input paste",
function () {
var limit = $(this).data("limit");
var remainingChars = limit - $(this).val().length;
if (remainingChars <= 0) {
$(this).val($(this).val().substring(0, limit));
}
$(this).closest('div').find(".countdown").text(remainingChars<=0?0:remainingChars);
});
});
# HTML
<textarea data-limit="3000"></textarea>
<span class="countdown">3000</span> Characters Remaining
source
share