1

I am implementing a character count for my form's textarea field. I have jQuery currently counting and writing my character statement using this code:

$("#textarea").keyup(function(){
    $("#count").text("Characters left: " + (500 - $(this).val().length));
});

However, I would like to know what is wrong with my JavaScript:

var textarea = document.forms["form"].elements.textarea;

textarea.addEventListener("keypress", textareaLengthCheck, false);

function textareaLengthCheck(textarea) {
var length = textarea.length;
console.log(length);
var charactersLeft = 500 - length;
console.log(charactersLeft);
count.innerHTML = "Characters left: " + charactersLeft;
}

The variable declaration and the event listener are in a jQuery document ready function. I am not sure what I am doing wrong for my event listener or function then.

Thanks

Ilan Biala
  • 3,319
  • 5
  • 36
  • 45

4 Answers4

3

The variable textarea is not present in the function's scope, you can refere to it with the this keyword

function textareaLengthCheck() {
    var length = this.value.length;
    var charactersLeft = 500 - length;
    var count = document.getElementById('count');
    count.innerHTML = "Characters left: " + charactersLeft;
}
wazz3r
  • 472
  • 4
  • 15
  • This result gives undefined in the console and in the actual printout, something else isn't working. – Ilan Biala Dec 29 '12 at 22:57
  • Sorry about that, I just copied your code. You need to use `this.value` to get the actual content. I've updated my answer. – wazz3r Dec 30 '12 at 14:24
2

The handler in your code is not passed the element involved with the event. It's passed an event object. Because you declared the function with a parameter, that hides the variable declared in the outer scope.

The problem is here:

function textareaLengthCheck(textarea) {

You've included a parameter named "textarea" in the function definition. That will be what the symbol means inside the function, so the outer variable also called "textarea" won't be visible inside that function.

Be aware that Firefox, Chrome, and Safari all report an incorrect length for <textarea> values. Those browsers report the length of the textarea as if all explicit line breaks (that is, places where the user hit the "Enter" key) as one character. In fact, when the browsers post a form with a <textarea> in it, all explicit line breaks are converted to two-character sequences (carriage return and line feed). Thus, if you're imposing a maximum length in order to prevent an overflow at the server, it won't work unless you treat line breaks as two characters. To do that, you have to take the value's length, and then add in the number of line breaks in the string (found with a regex or something).

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • 1
    @IlanBiala well it looks OK to me :-) However I edited it to add the specific issue. – Pointy Dec 29 '12 at 22:46
  • is not passed with... what does that mean? – Ilan Biala Dec 29 '12 at 22:56
  • @IlanBiala when the event handler is called, the browser passes an event object as the parameter. It does not pass the element involved with the event (well, the element is passed, but as a property of the event object). Your code is written as if the parameter is the DOM element itself, and it is not. – Pointy Dec 29 '12 at 23:31
0

since you are using jquery

how about a plugin

http://keith-wood.name/maxlength.html

$("#limited-textarea").maxlength({
    max: 400
});
Kevin Florenz Daus
  • 597
  • 3
  • 9
  • 23
  • sorry, but 6 lines vs 3 and an extra file isn't a good trade, good thought though, for something more difficult or intensive, a good plugin could do the trick – Ilan Biala Apr 03 '13 at 00:34
0

var maxchar = 10;
$('#message').after('<span id="count" class="counter"></span>');
$('#count').html(maxchar+' of '+maxchar);
$('#message').attr('maxlength', maxchar);
$('#message').parent().addClass('wrap-text');
$('#message').on("keydown", function(e){
 var len =  $('#message').val().length;
 if (len >= maxchar && e.keyCode != 8)
     e.preventDefault();
 else if(len <= maxchar && e.keyCode == 8){
  if(len <= maxchar && len != 0)
      $('#count').html(maxchar+' of '+(maxchar - len +1));
     else if(len == 0)
      $('#count').html(maxchar+' of '+(maxchar - len));
 }else
   $('#count').html(maxchar+' of '+(maxchar - len-1)); 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="message"></textarea>