最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Textarea Character Count using JavaScript - Stack Overflow

programmeradmin7浏览0评论

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

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

Share Improve this question asked Dec 29, 2012 at 22:40 Ilan BialaIlan Biala 3,4195 gold badges37 silver badges45 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 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;
}

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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="message"></textarea>

发布评论

评论列表(0)

  1. 暂无评论