JavaScript Error with form counter that was working fine

Hello,

Maybe it is because I am way too tired as been working non-stop for days :slight_smile:
But for whatever reason I cannot find out why all of a sudden the JS code on a part of a form is not working.

The code is supposed to count the number of Characters in a textarea field and not allow the user to type
more than the allotted number of Characters. The JS code is:

function textCounter_rem(maxlimit) {

var field = document.getElementById("description");
var cntfield = document.getElementById("rem");
if (field.value.length > maxlimit) {
	field.value = field.value.substring(0, maxlimit);}
else
	{cntfield.value = maxlimit - field.value.length;}

}

But all of a sudden it is not working!

You can see a sample client page here:

As you can see when you type in the field for: Description of this Site
The ā€œNumber of Characters remainingā€ field is not changing and the user can type past 200!

Thanks.

The page requires a login.

Remember that JS can be turned off, too, so you might want to set a maxlength=ā€œ200ā€ attribute in the HTML itself, or set a limit in the PHP behind the scenes.

Hello ralph.m,

Sorry for providing a URL that was password protected.
Here is the problem in a non-password protected mode:

So again the problem is the counter on the ā€œDescription of this Siteā€ textarea is all of a sudden not working.
That is not counting as users type in and thus stopping them when they hit the maximum allowed 200 Chars!

ThanX,

I donā€™t know enough JS to help with this, so weā€™ll have to wait for someone to step in. However, just looking at the code, I would expect field.value.length to evaluate to 3, rather than 200ā€”if I understand .length properly. That may be a problem.

Viewing that page the error console reports:

Uncaught exception: TypeError: Cannot convert ā€˜fieldā€™ to object
Error thrown at line 159, column 1 in textCounter_rem(maxlimit) in http://www.anoox.com/submit_confirm_new.php?w=20121271231172891:
if (field.value.length > maxlimit)

So opening the debugger (which all browsers except Firrefox have built in) and it shows that field = null.

Given that field was assigned the value document.getElementById(ā€˜descā€™) a couple of lines earlier this means that there is no id=ā€œdescā€ in the page - which there isnā€™t as the field you are trying to validate has id=ā€œdescriptionā€.

Dang, Iā€™m sure thatā€™s changed since I looked at the page. :rolleyes: I was sure all the IDs matched.

Nice explanation of the steps, felgall.

Thanks.
Figured out what the problem was.
We had 2 definition of the same JS function!

Cheers :slight_smile: