开发者

Textarea count + Max Length

开发者 https://www.devze.com 2023-04-03 17:30 出处:网络
I\'m trying to creating a little JavaScript Textcount which wi开发者_Go百科ll include Maxarea. Here is my JS:

I'm trying to creating a little JavaScript Textcount which wi开发者_Go百科ll include Maxarea.

Here is my JS:

function maxlength(item, max){
var a = $('#'+item+'').val();
var q = eval(""+a+".length");
var l = q - max
var msg = "Sorry but the max is "+max+", You have entered "+q+" characters into the textarea. Please delete at least "+l+" characters."
if (q > max){
$('#limit').html(msg);
}
}

With that this is the HTML:

<textarea id="area" onkeyup="maxlength('area', 12)"></textarea>
<br><br>
<div id="limit"></div>

The Problem is that the limit is not showing.


You need to lose the eval, var q = eval(""+a+".length");, and replace with this:

var q = a.length;

a is already a string with a length property.

Example


You should have a look at this jQuery Plugin : http://unwrongest.com/projects/limit/

I've found it to be very useful and easy to implement.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号