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
    New Coder
    Join Date
    May 2007
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post

    Adding OPTION to top of SELECT

    Call me a noob ...
    When I write the following code
    Code:
    FacilitySelectBox[0] = new Option(" -- Please select a facility -- ");
    I pretty much expect it to add a new (per the NEW keyword) value to the [0]th index of my select. Why does it not do that? Instead, it overwrites whatever value happens to be at [0].

    I have a SELECT box that I've subsequently filled with OPTIONs. What I want to do next is insert "select a facility" as the first value. This value may or may not be inserted, depending on the number of values already listed, otherwise I'd be tempted to add a placeholder value at [0].

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,505
    Thanks
    3
    Thanked 501 Times in 488 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <select id="tst" >
    <option>option 1</option>
    <option>option 2</option>
    </select>
    
    <script type="text/javascript">
    /*<![CDATA[*/
    
    function AddOption(id,txt){
     var obj=document.getElementById(id),o=document.createElement('OPTION');
     o.innerHTML=txt;
     obj.insertBefore(o,obj.firstChild);
     obj.selectedIndex=0;
    }
    
    AddOption('tst',' -- Please select a facility -- ');
    /*]]>*/
    </script>
    
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • Users who have thanked vwphillips for this post:

    SlowCoder (12-14-2012)

  • #3
    New Coder
    Join Date
    May 2007
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Thank you. Will review it.


  •  

    Posting Permissions

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