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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Sep 2002
    Posts
    143
    Thanks
    0
    Thanked 0 Times in 0 Posts

    unselect single option in select box

    hello
    i have this simple script i only need to unselect the selected option box
    with no luck ..
    what is wrong here ?
    <HTML>
    <HEAD>
    <script>
    function foo(){
    var oSelect=document.oForm.blah;
    for(var i=0;i<oSelect.length;i++){
    oSelect.options[i].selected=false;
    }
    }
    </script>
    <TITLE> New Document </TITLE>
    </HEAD>
    <form name="oForm">
    <SELECT NAME="blah" size="5" style="width:20px">
    <option >1</option>
    <option selected>2</option>
    <option>3</option>
    </SELECT>
    </form>
    <input type=button value="Unselect" onclick="foo();">
    </BODY>
    </HTML>

    thanks!

  • #2
    Regular Coder
    Join Date
    Sep 2002
    Location
    Louisiana
    Posts
    576
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i don't think you can unselect an option in a select box once something has been selected, make an option like this
    <option>-----------</option>
    and move the focus to that like this
    oSelect.options[0].focus();

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    if the select is only single select (not multiple), just set the selectedIndex to -1 without doing a loop.


    <input type=button value="Unselect" onclick="this.form.blah.selectedIndex=-1">
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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