I do not quite understand the $ wordCount variable. I believe you can do the following:
$("#edit-field-message-0-value").keyup(function() { var $this = $(this); var wordcount = $this.val().split(/\b[\s,\.-:;]*/).length; if (wordcount > $limitWords) { $this.addClass('error'); } else { $this.addClass('not-error'); } });
I also assume that you want to keep the current counter:
$("#edit-field-message-0-value").keyup(function() { var $this = $(this); var wordcount = $this.val().split(/\b[\s,\.-:;]*/).length; if (wordcount > $limitWords) { $('#count').html($limitWords); $this.addClass('error'); } else { $('#count').html(wordcount); $this.addClass('not-error'); } });
Corey ballou
source share