How to preserve cursor position

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dj12345
    New Member
    • Jan 2008
    • 14

    How to preserve cursor position

    i have function to make character in lowercase if a text is in uppercase


    Code:
       function upperCase(x)
        {   
            try
            {
                var y=document.getElementById(x).value;
                document.getElementById(x).value=y.toLowerCase();
            }
            catch(Error)
            {
            }
        }

    i am calling
    Code:
    <asp:TextBox ID="txtUrl" Width="450px" runat="server" Wrap="true" onkeyup="upperCase(this.id)" TextMode="MultiLine"></asp:TextBox>

    but whenever i type anything withing typed text for eg. if the textbox already have string "texttextte xt" n if i try to modify the middle character then on upperCase function call the the function male the character to lowercase in then cursor position goes to end of the text... so i am not able to type anything in the middle... so what is the option to preserve the cursor position at it is which was befor..
    Last edited by Dormilich; Mar 17 '09, 09:20 AM. Reason: added [code] tags
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    the only solution on my mind is changing the event (onchange, maybe onblur). the reason (I think) for the behaviour is that the value becomes re-written (well first deleted). maybe someone know how to position the cursor in a string.....

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      That's what I would suggest. However, if you insist on using onkeyup, have a play with this (taken from this thread).

      Comment

      Working...