|
|
 |
Re: FN-FORUM: Javascript error
date posted 21st September 2007 11:50
ADH WebCreations wrote:
> Hello,
>
> This will be a easy for you guys.
>
> I have a textbox in a form which only allows 1000 characters and I've
> seen on a forum I installed on the site a character countdown and I want
> to use that for my textbox.
>
> The javascript code is:
>
>
> var noalert = true, gralert = false, rdalert = false, clalert = false;
> var prevsec = 5
> var prevtxt
> var cntsec = 0
>
> function tick() {
> cntsec++
> calcCharLeft()
> timerID = setTimeout("tick()",1000)
> }
>
> var autoprev = false
> var topicfirst = true;
>
> function enabPrev() {
> if ( autoprev == false ) {
> autoprev = true
> topicfirst = true
> document.queryform.Summary.focus();
> autoPreview();
> }
> else {
> autoprev = false;
> ubbstr = '';
> document.queryform.Summary.focus();
> }
> calcCharLeft()
> }
> function calcCharLeft() {
> clipped = false
> maxLength = 1000
> if (document.queryform.Summary.value.length > maxLength) {
> document.queryform.Summary.value =
> document.queryform.Summary.value.substring(0,maxLength)
> charleft = 0
> clipped = true
> } else {
> charleft = maxLength - document.queryform.Summary.value.length
> }
> prevsec++
> if(autoprev && prevsec > 5 && prevtxt !=
> document.queryform.Summary.value) {
> autoPreview()
> prevtxt = document.queryform.Summary.value
> }
> }
>
>
>
> and on the textbox I have:
>
I assume there is not a
before this? That would be the error.
> onclick="javascript:calcCharLeft(this);"
> onkeyup="javascript:calcCharLeft(this);"
> onchange="javascript:calcCharLeft(this);">
> Left
>
> I understand bits of javascript but it keeps coming up with an error
> saying:
>
> if (document.queryform.Summary.value.length > maxLength) { is null or
> not an object"
>
> Can anyone help?
How about this code instead?
function LimitTextArea(e,limit)
{
var div =document.createElement('div');
div.id=id+'_charsrem';
div.className='charsrem';
e.parentNode.appendChild(div);
div.style.width=(e.clientWidth-5)+'px';
e.onchange=function() {
if(e.value.length>limit)
{
var sub=e.value;
sub=sub.substring(0,limit);
e.value=sub;
}
div.innerHTML='You may enter a further '+(limit-e.value.length)+'
characters.';
}
e.onkeyup=e.onchange;
e.onchange();
}
You will want some css to make the counter look nice, like this:
div.charsrem {
border-left:1px solid #aaa;
border-right:1px solid #aaa;
border-bottom:1px solid #aaa;
font-size:11px;
background-color:#efefef;
padding: 3px;
font-family:Verdana,Arial;
}
And the text area code would be:
I personally think my code is nicer, since it doesn't display a
disfunctional countdown if javascript is turned off. Of course you would
want to check the text posted to the server in case Javascript is turned
off.
--
Artumi Systems, 58 Salmon Street, Sheffield, S11 8DD.
Tel 0114 250 7654, Web http://www.artumi.com
VAT Reg 889 0317 88
|
 |
|