Last active
August 29, 2015 14:03
-
-
Save daltonrooney/d59b396f71f3efa6003a to your computer and use it in GitHub Desktop.
Visual character limit for textareas
This file contains 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
(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); |
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
We use this for Advanced Custom Fields, you may need to adjust depending on your markup.