Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save New0/dbb6c14d8e950fcaa10f2d4d8adcd623 to your computer and use it in GitHub Desktop.
Save New0/dbb6c14d8e950fcaa10f2d4d8adcd623 to your computer and use it in GitHub Desktop.
Count words in a textarea field and print the value in a hidden field
$(document).ready(function() {
//CHANGE THE IDs TO MATCH YOUR FIELDS, these are frontend IDs, usually the field ID looking like fld_7683514 will be transformed as #fld_7683514_1
var textarea = "#fld_7683514_1"; //ID of the textarea we want to count words
var hidden_value = '#fld_9053892_1'; //ID of the hidden field we want to print count in value parameter
$(textarea).keyup(function() { word_count( textarea, hidden_value ) });
});
function word_count(field, count) {
var number = 0;
var matches = $(field).val().match(/\b/g);
if(matches) {
number = matches.length/2;
}
$(count).val( number );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment