Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Feb 2003
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts

    A Tricky textarea delete problem

    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?

    PHP Code:
    <cfset remainingChars "60">

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

    </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> 

  • #2
    New Coder
    Join Date
    Feb 2003
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    New Coder TripperTreats's Avatar
    Join Date
    Oct 2006
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Psychedelic digital art at www.trippertreats.com.

    "And in the end, the love you take
    is equal to the love you make
    ."

  • #4
    New Coder
    Join Date
    Feb 2003
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    New Coder TripperTreats's Avatar
    Join Date
    Oct 2006
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

    Code:
    document.getElementById(opid).value = (re-nu).toFixed(2)
    Psychedelic digital art at www.trippertreats.com.

    "And in the end, the love you take
    is equal to the love you make
    ."

  • #6
    New Coder
    Join Date
    Feb 2003
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    New Coder
    Join Date
    Feb 2003
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi TripperTreats,

    I hope it was nothing I said :-D

    jo


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •