How to unselect a text of an input box

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stefan Mueller

    How to unselect a text of an input box

    I've an input box
    <input type = "text" name = "MyInput" value = "">

    and a selection
    <select name = "MySelectio n" size = "1">
    <option value = "1">Entry 1
    <option value = "2">Entry 2
    </select>

    With
    document.MyForm .MyInput.focus( );
    document.MyForm .MyInput.select ();

    I can set the focus to the input box and select the whole text. But if I set
    the focus to the selection box with
    document.MyForm .MySelection.fo cus();

    the text in the input box is still selected.

    How can I unselect this text?
    Stefan

    PS: One possibility would be to add a hidden input box and to set the focus
    to this hidden box before I set the focus to the selection. But I think this
    would only be a workaround.


  • McKirahan

    #2
    Re: How to unselect a text of an input box

    "Stefan Mueller" <seekware-remove-@yahoo.com> wrote in message
    news:dmch7p$2bu f$1@news.imp.ch ...[color=blue]
    > I've an input box
    > <input type = "text" name = "MyInput" value = "">
    >
    > and a selection
    > <select name = "MySelectio n" size = "1">
    > <option value = "1">Entry 1
    > <option value = "2">Entry 2
    > </select>
    >
    > With
    > document.MyForm .MyInput.focus( );
    > document.MyForm .MyInput.select ();
    >
    > I can set the focus to the input box and select the whole text. But if I[/color]
    set[color=blue]
    > the focus to the selection box with
    > document.MyForm .MySelection.fo cus();
    >
    > the text in the input box is still selected.
    >
    > How can I unselect this text?
    > Stefan
    >
    > PS: One possibility would be to add a hidden input box and to set the[/color]
    focus[color=blue]
    > to this hidden box before I set the focus to the selection. But I think[/color]
    this[color=blue]
    > would only be a workaround.
    >[/color]

    Will this help?

    <html>
    <head>
    <title>blur.htm l</title>
    <script type="text/javascript">
    function onloader() {
    document.MyForm .MyInput.focus( );
    document.MyForm .MyInput.select ();
    }
    function clearSelection () {
    if (document.selec tion)
    document.select ion.empty();
    else if (window.getSele ction)
    window.getSelec tion().removeAl lRanges();
    }
    </script>
    </head>
    <body onload="onloade r()">
    <form action="" method="get" name="MyForm">
    <input type="text" name="MyInput"
    value="Hello World" onblur="clearSe lection()">
    <select name="MySelecti on" size="1">
    <option value="1">Entry 1
    <option value="2">Entry 2
    </select>
    </form>
    </body>
    </html>


    Credit
    URL:http://groups.google.com/group/comp...._thread/thread
    /871226cbeb74ae7 0/d3a48c14f66cc70 7?lnk=st&q=Java Script+deselect +text&rnum=8&
    hl=en#d3a48c14f 66cc707


    Comment

    • Stefan Mueller

      #3
      Re: How to unselect a text of an input box

      > function clearSelection () {[color=blue]
      > if (document.selec tion)
      > document.select ion.empty();
      > else if (window.getSele ction)
      > window.getSelec tion().removeAl lRanges();
      > }[/color]

      With Internet Explorer and Firefox is works, but not with Opera.

      Opera error message:

      Event thread: blur
      Error:
      name: TypeError
      message: Statement on line 11: Type mismatch (usually a non-object value
      used where an object is required)
      Backtrace:
      Line 11 of inline#1 script in file://localhost/D:/test.html
      document.select ion.empty();
      Line 1 of script
      clearSelection( );
      At unknown location
      [statement source code not available]


      Comment

      • Stefan Mueller

        #4
        Re: How to unselect a text of an input box

        > With Internet Explorer and Firefox is works, but not with Opera.

        I've tried many solutions to unselect a text of an input box. To only
        solution I found which works on all browsers is

        var temptext = document.MyForm .MyInput.value;
        document.MyForm .MyInput.value = "";
        document.MyForm .MyInput.value = temptext;

        Stefan


        Comment

        Working...