Character counter breaks after adding EmojiOne Area

Jordan Benett
Jordan Benett used Ask the Experts™
on
I have a character counter for my textarea that was working great until I added EmojiOne Area. The emoji picker now works great, but my character counter stopped working. For some reason the keyup is no longer working off of the ID.

HTML

<textarea id="message" class="form-control" placeholder="Enter text here..."></textarea> 
<label><span id="chars" class="lead">140</span></label> characters left

Open in new window


JavaScript

/*emojioneArea */
$(document).ready(function() {
  $("#message").emojioneArea({
    pickerPosition: "bottom",
    tonesStyle: "bullet"
  });
});

/*Character Counter */
function countChar(val) {
  var len = val.value.length;

  if (len >= 140) {
    val.value = val.value.substring(0, 140);
    $('#stat span').text(0);
  } else {
    $('#stat span').text(140 - len);
  }
}
countChar($('#message').get(0));
$('#message').keyup(function() {
  countChar(this);
});

Open in new window


JSFiddle.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Full Stack Developer
Commented:
As I can see there are few problems with you code. So I corrected the same and following is the updated working code with emojionearea and character count.
FYI - I came to got that, the same problem was also asked over the Stackoverflow and the below solution worked fine there as well. And Hope it will work for you as well.

/*emojioneArea */
   $(document).ready(function() {
         $("#message").emojioneArea({
                       pickerPosition: "bottom",
                       tonesStyle: "bullet",
                       events: {
                         keyup: function (editor, event) {
                           console.log('event:keyup');
                           countChar(this);
                        }
                      }
               });
    }); 

   /*Character Counter */
        function countChar(val) {
            var len = val.getText().length;
            if (len >= 140) {
                  val.value = val.content.substring(0, 140);
                  $('#chars').text(0);
            } else {
                 $('#chars').text(140 - len);
            }
        }

Open in new window

Author

Commented:
Banshi, thank you for the solution, can you please share the Fiddle link, as I am not able to run it.
Banshi lal dangiFull Stack Developer
Commented:
Sure Jordan, please find the fiddle link - Fiddle

Author

Commented:
Thanks a lot Banshi, That worked great!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial