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
    Regular Coder
    Join Date
    Jan 2006
    Location
    Preston, Lancashire, England
    Posts
    285
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem with setSelectionRange in IE

    hello i have this very simple code
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
              "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>boo</title>
    <script language="javascript">
    
    function movemiddle () {
     var control = document.getElementById("textbox360");
     control.focus();
     control.setSelectionRange(5, 5);
     
    }
    </script>
    </head>
    <body>
    
    <input id="textbox360" type="text"><input type="button" onClick="movemiddle()">
    </body>
    </html>
    wen i run it in mozilla the caret jumps to the correct place. But in IE i get

    "A Runtime Error has occured.
    Do you wish to Debug?

    Line:11
    Error: Opject doesn't support this property or method."

    Does anyone know if this is becuase IE doesn't have the setSelectionRange function or am i don'tt something wrong.

    Thank you for your time
    MRMAN

  • #2
    New Coder
    Join Date
    Jun 2006
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Try this?

    Try this?

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
              "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>boo</title>
    <script language="javascript">
    
    function setSelectionRange(input, selectionStart, selectionEnd) 
    
    {
      if (input.setSelectionRange) {
        input.focus();
        input.setSelectionRange(selectionStart, selectionStart);
      }
      else if (input.createTextRange) {
        var range = input.createTextRange();
        range.collapse(true);
        range.moveEnd('character', selectionEnd);
        range.moveStart('character', selectionStart);
        range.select();
      }
    }
    
    
    function movemiddle () {
     var control = document.getElementById("textbox360");
     setSelectionRange(control,5, 5);
     
    }
    </script>
    </head>
    <body>
    
    <input id="textbox360" type="text"><input type="button" onClick="movemiddle()">
    </body>
    </html>

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Location
    Preston, Lancashire, England
    Posts
    285
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much. its fantastic.


    Can i trouble you to ask how i can then get the chracters from the caret to the beginning of the string

    so i would get MRM from MRM|AN

    Thanks
    Last edited by MRMAN; 06-30-2006 at 03:06 PM.

  • #4
    New Coder
    Join Date
    Jan 2006
    Location
    Oslo, Norway
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This will at least point you in the correct direction: http://parentnode.org/javascript/wor...rsor-position/
    Drømmejenta: Kristen jente som progger Python!

  • #5
    New Coder
    Join Date
    Jun 2006
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Sorry for taking so long to reply, wasnt online this weekend...

    You simply need to change a VERY small thing

    in the setSelectionRange function, simply change the
    first part to this:

    Code:
      if (input.setSelectionRange) {
        input.focus();
        input.setSelectionRange(selectionStart, selectionEnd);
      }
    You'll see that input.setSelectionRange changed a little.

    Simply specify which part you want to select in your input string now

  • #6
    Regular Coder
    Join Date
    Jan 2006
    Location
    Preston, Lancashire, England
    Posts
    285
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much for your help

  • #7
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,963
    Thanks
    120
    Thanked 76 Times in 76 Posts
    just corrected above code, thanks CSTruter

    Code:
    function setSelectionRange(input, selectionStart, selectionEnd) 
    
    {
      if (input.setSelectionRange) {
        input.focus();
        input.setSelectionRange(selectionStart, selectionEnd);
      }
      else if (input.createTextRange) {
        var range = input.createTextRange();
        range.collapse(true);
        range.moveEnd('character', selectionEnd);
        range.moveStart('character', selectionStart);
        range.select();
      }
    }


  •  

    Posting Permissions

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