Skip to content

Instantly share code, notes, and snippets.

@eteubert
Created December 2, 2011 10:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eteubert/1422754 to your computer and use it in GitHub Desktop.
Save eteubert/1422754 to your computer and use it in GitHub Desktop.
WordPress: Comment Length Limiter
jQuery(function($) {
// configure
var comment_input = $( '#commentform textarea' );
var submit_button = $( '#commentform .form-submit' );
var comment_limit_chars = 1000;
// stop editing here
// display how many characters are left
$( '<div class="comment_limit_info"><span>' + comment_limit_chars + '</span> characters left</div>' ).insertAfter( comment_input );
comment_input.bind( 'keyup', function() {
// calculate characters left
var comment_length = $(this).val().length;
var chars_left = comment_limit_chars - comment_length;
// display characters left
$( '.comment_limit_info span' ).html( chars_left );
// hide submit button if too many chars were used
if (submit_button)
( chars_left < 0 ) ? submit_button.hide() : submit_button.show();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment