Question

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

Was it helpful?

Solution

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;
}

OTHER TIPS

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).

since you are using jquery

how about a plugin

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

$("#limited-textarea").maxlength({
    max: 400
});

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>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top