Szilard Magyar Szilard Magyar - 5 months ago 21
Javascript Question

jQuery keyup event fires only once

Why does this jQuery event only fire once?

$(document).on('keyup', '[data-behavior="oneliner-text-area"]', function (event) {
var textLength = $(this).length;
var textLengthLimit = 140;
$('[data-behavior="oneliner-counter"]').html(textLengthLimit - textLength);
if ((textLengthLimit - textLength) < 0) {
$('[data-behavior="oneliner-counter').addClass('oneliner-count-warning');
}
});

Answer

You are not counting textfield's text length, you are counting number of elements, and that is it always be 1 so you will always get 139.

$(document).on('keyup', '[data-behavior="oneliner-text-area"]', function (event) {
  var textLength = $(this).val().length; // Here is a change
  var textLengthLimit = 140;
  $('[data-behavior="oneliner-counter"]').html(textLengthLimit - textLength);
  if ((textLengthLimit - textLength) < 0) {
    $('[data-behavior="oneliner-counter').addClass('oneliner-count-warning');
  }
});
Comments