admin管理员组文章数量:1322010
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 04 Answers
Reset to default 3The 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>
本文标签: jqueryTextarea Character Count using JavaScriptStack Overflow
版权声明:本文标题:jquery - Textarea Character Count using JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742110198a2421215.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论