Shawn31313
Shawn31313

Reputation: 6052

Textarea count + Max Length

I'm trying to creating a little JavaScript Textcount which will 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.

Upvotes: 1

Views: 820

Answers (2)

DaveE
DaveE

Reputation: 1643

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.

Upvotes: 0

Joe
Joe

Reputation: 82594

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

Upvotes: 1

Related Questions