...

View Full Version : A Tricky textarea delete problem



ojoonline
01-10-2007, 01:10 AM
Hi,

I have a textarea with a counter that stops you being able to enter any more characters when it reaches 0. The characters remaining is displayed in a textfield. It works perfectly.

http://www.ojoonline.com.au/testbeds/x-nodelete.cfm

The problem is that when you enter the limit of characters and the counter displays '0' and you highlight a word in the textarea and click delete on your keyboard the whole textarea is deleted. eeeccchhttt.

This is very frustrating for users to learn the hard way. Can anything be done?



<cfset remainingChars = "60">

<script language="JavaScript" type="text/javascript">
function textCounter(field, maxlimit) {
if (field.value.length > maxlimit)
field.value = field.value.substring(0, maxlimit);
}
</script>

<script language="JavaScript" type="text/javascript">
function Calculate(obj,m,opid){
nu=obj.value.length;
<cfoutput>
re=#remainingChars#
</cfoutput>
document.getElementById(opid).value=re-nu.toFixed(2);
}
</script>


<form action = "testbeds/x-nodelete.cfm" method="post" name="f">
<cfoutput>
Message:<br>
<textarea id="ip" name="Message_Text" onKeyDown="textCounter(this.form.Message_Text, #evaluate(remainingChars-1)#);Calculate(this,1,'op');" onkeyup="Calculate(this,1,'op');" onblur="Calculate(this,1,'op');" rows="5" cols="30"></textarea>
<br>
Characters remaining for message:
<input id="op" class="counterField" value="#remainingChars#" size="2">
</cfoutput>
<input type="submit" name="Submit" value="Send" class="btnReg">
</form>

ojoonline
01-12-2007, 03:39 AM
Actually, if any text is selected in the textarea and then backspaced, deleted or typed over the whole lot is deleted.

Perhaps there is a way to stop the contents of the textarea from being selected? Is this possible?

thanks,

jo

TripperTreats
01-12-2007, 03:42 AM
I'm not sure why you're having this problem. To completely prevent selection of the textarea contents, but still allow clicking inside it (because they need to click it to edit it), consider any of the following, to be placed inside the textarea tag:

ondragstart = return false;
onselect = return false;
ondblclick = return false;

These might be what you are looking for.

ojoonline
01-12-2007, 04:09 AM
thanks Tripper. I've no idea why it deletes the whole textarea either. Only does it when the counter is 0. Very strange and annoying.

Thanks for your suggestions but none of them worked.

jo

TripperTreats
01-12-2007, 04:41 AM
We can solve this. First, a little discussion.

The behavior only occurs in IE, not FF.
Do you mean for a carriage return to count as a character? In FF whenever you hit return the counter is decremented by 1, in IE by 2.
When the character limit is reached, do you want the next letter pressed to replace the last letter (which it currently does) or to just not do anything at all?
In function Calculate, what is m? It doesn't look like you use it.
In function Calculate, why do you use .toFixed(2)? This makes the number have two decimal places (which will be .00 since this is an integer). Seems unnecessary. Also, if you really want to have those you should use


document.getElementById(opid).value = (re-nu).toFixed(2)

ojoonline
01-12-2007, 04:56 AM
Hi TripperTreats,

thanks. I found both functions on this site and have tried to edit them to suit my form.

In answer to your questions:

>The behavior only occurs in IE, not FF.

That would be right!

>Do you mean for a carriage return to count as a character? In FF whenever you hit return the counter is decremented by 1, in IE by 2.

No. I don't think it should count as a character. Would be good to disable the return / new line.

>When the character limit is reached, do you want the next letter pressed to replace the last letter (which it currently does) or to just not do anything at all?

I don't mind it replacing the last letter but would probably be better if it didn't do anything at all.

>In function Calculate, what is m? It doesn't look like you use it.
In function Calculate, why do you use .toFixed(2)? This makes the number have two decimal places (which will be .00 since this is an integer). Seems unnecessary.

I don't think it is being used.

thanks TripperTreats.

jo

ojoonline
01-16-2007, 02:48 AM
Hi TripperTreats,

I hope it was nothing I said :-D

jo



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum