Skip to content

Instantly share code, notes, and snippets.

@daltonrooney
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save daltonrooney/d59b396f71f3efa6003a to your computer and use it in GitHub Desktop.

Select an option

Save daltonrooney/d59b396f71f3efa6003a to your computer and use it in GitHub Desktop.
Visual character limit for textareas
(function($){
function addCharLimit(){
$('textarea').each(function(){
maxlength = $(this).attr("maxlength");
if ( maxlength !== undefined ) {
charContainer = $(this).parent().find('p.charleft').val();
if ( charContainer === undefined ) {
$(this).parent().append("<p class='charleft description'>");
}
charleft = maxlength - $(this).val().length;
$(this).parent().find('.charleft').html(charleft+" characters left.");
}
$(this).keyup(function() {
maxlength = $(this).attr("maxlength");
if ( maxlength !== undefined ) {
charleft = maxlength - $(this).val().length;
$(this).parent().find('.charleft').html(charleft+" characters left.");
}
})
});
}
acf.add_action('ready append', function( $el ){
addCharLimit();
});
})(this.jQuery);
@daltonrooney
Copy link
Copy Markdown
Author

We use this for Advanced Custom Fields, you may need to adjust depending on your markup.

@daltonrooney
Copy link
Copy Markdown
Author

Updated to support dynamically loaded ACF textareas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment