Guest User

Example of Text Counter in Rails

a guest
Jan 16th, 2016
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var ready;
  2. ready = function() {
  3.   var charsCountEl, countTextBox, totalChars;
  4.   totalChars = 100;
  5.   countTextBox = $('#body-field');
  6.   charsCountEl = $('#countchars');
  7.   charsCountEl.text(totalChars);
  8.   countTextBox.keyup(function() {
  9.     var CharsToDel, thisChars;
  10.     thisChars = this.value.replace(/{.*}/g, '').length;
  11.     if (thisChars > totalChars) {
  12.       CharsToDel = thisChars - totalChars;
  13.       this.value = this.value.substring(0, this.value.length - CharsToDel);
  14.     } else {
  15.       charsCountEl.text(totalChars - thisChars);
  16.     }
  17.   });
  18. };
  19.  
  20. $(document).ready(ready);
  21. $(document).on('page:load', ready);
Add Comment
Please, Sign In to add comment