[thelist] javascript counter for remaining characters

Nicky Thompson nicky.thompson at Rightmove.co.uk
Thu Feb 20 09:54:07 CST 2003


--
[ Picked text/plain from multipart/alternative ]
hi

I'm trying to make a little counter which shows you how many characters you
have remaining to type - think sending SMS online. the counter works, but I
can't seem to get the counter to display the number of chars left right at
the beginning - 0 if it's the first time, or whatever it is if I'm editing
this text box and it's already got a value, as opposed to starting from the
beginning.

the counter:

function taLimit() {
 var taObj=event.srcElement;
 // no keypresses after maxlength
 if (taObj.value.length==taObj.maxLength*1) return false;
}

// count & display how many chars are left
function taCount(visCnt) {
 var taObj=event.srcElement;
 // if there's more than allowed, chop off the end
 if (taObj.value.length>taObj.maxLength*1)
taObj.value=taObj.value.substring(0,taObj.maxLength*1);
 // change the number in the counter span
 if (visCnt) visCnt.innerText=taObj.maxLength-taObj.value.length;
}

<textarea onkeypress="return taLimit()" onkeyup="return taCount(visCnt)"
name="pr_d" cols=92 rows=10 maxLength="32000"> blah blah </textarea>

remaining characters: <span id="visCnt">32000</span>

the above code all works frin in Mozilla and IE6. so now I come to my
problem, which i thought this might solve:

function initLength() {
 var taObj=event.srcElement;
 initLength = (taObj.value.length - taObj.maxLength*1);
 document.writeln('initLength');
}

which i then call like:
<span id="visCnt"><script language=JavaScript type=text/JavaScript
src=/b/js/initLength.js></script></span>
ooh! ugly! and it doesn't work anyway. okay, so I tried changing that last
line

document.writeln('initLength');

to

if (visCnt) visCnt.innerText = initLength;

and nada :(
how about: <span id="counter"
onload="javascipt:initLength2(visCnt);"></span>
(initLength2 is the initLength function with the different last line)

nope. I wonder if anyone could throw an eye over this and see where I'm
going wrong?
Many TIA
nicky


________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs service.
________________________________________________________________________



More information about the thelist mailing list