|
|
 |
Re: FN-FORUM: Javascript error
date posted 21st September 2007 12:57
Here is some code I've used in the past, it works off a timer rather
than keypresses:
var maxChars = 200;
var canCount = false;
var counterTimer = null;
function countChars()
{
if (counterTimer) clearTimeout(counterTimer);
var numberOfChars = document.theForm.theTextArea.value.length;
document.getElementById("theCounter").innerHTML = "Chars left: " +
(maxChars - numberOfChars)
if ((maxChars - numberOfChars ) < 0)
{
// this could be changed to
//document.theForm.theTextArea.value =
document.theForm.theTextArea.value.substring(0, maxChars);
document.getElementById("theCounter").innerHTML = "You have entered
too much text, please reduce it";
}
if (canCount)
{
counterTimer = setTimeout("countChars()", 250);
}
}
then the textarea:
and to display the counter
Martin
On 21/09/2007 13:18, ADH WebCreations wrote:
> Hi,
>
> I'm not getting any error messages with this but it's not working
> either...!
>
> Any ideas?
>
> Thanks
> Ashley
>
> -----Original Message-----
> From: [EMAIL REMOVED] [EMAIL REMOVED] On Behalf Of Dai
> Williams
> Sent: 21 September 2007 13:09
> To: FN-FORUM / [EMAIL REMOVED]
> Subject: RE: FN-FORUM: Javascript error
>
>
> ADH WebCreations wrote:
>> and on the textbox I have:
>>
>> > onclick="javascript:calcCharLeft(this);"
>> onkeyup="javascript:calcCharLeft(this);"
>> onchange="javascript:calcCharLeft(this);">
>> Left > />
>>
>> if (document.queryform.Summary.value.length > maxLength) { is null or
>> not an object"
>
> Since you are passing the object reference into the function it is much
> cleaner, safer and more extensible to refer to it using that object
> reference e.g.
>
> function calcCharLeft(obj) {
> clipped = false;
> maxLength = 1000;
> if (obj.value.length > maxLength) {
> obj.value = obj.value.substring(0,maxLength);
> charleft = 0;
> clipped = true;
> } else {
> charleft = maxLength - obj.value.length;
> }
> prevsec++;
> if(autoprev && prevsec > 5 && prevtxt != obj.value) {
> autoPreview();
> prevtxt = obj.value;
> }
> }
>
> Can't comment whether that is the only issue, but it is certainly worth
> a
> try. Also a good idea to get in the habit of terminating statements
> with ;
> even when it is not an absolute requirement.
>
> HTH,
>
> Dai
>
>
|
 |
|