SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict jamesbond's Avatar
    Join Date
    Feb 2001
    Location
    The Netherlands
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    default value in select list

    I'm trying to figure out how to preselect a country in a form, I have this so far:
    --------------------
    <form name=order>
    <select name=country>
    <option value="aaa">aaa</option>
    <option value="aaa">bbb</option>
    <option value="bbb">ccc</option>
    </select>
    </form>
    <script>
    document.order.country.selectedIndex=1;
    </script>
    ---------------------

    But I would like to be able to preselect a country based on the option value.
    Something like (this doesn't work obviously):

    document.order.country.selected.option='bbb';


    The default value for the country will be supplied by a previous form, so it changes depending on what has been selected in the previous form.
    Last edited by jamesbond; Mar 4, 2002 at 08:59.

  2. #2
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure how you are getting the value from the other form, but maybe this will help you.
    <html>
    <head>
    <title>Drop Down Test</title>
    <SCRIPT LANGUAGE='JavaScript'>
    function setsel()
    {
    optval="One"
    len=document.dropdown.droptest.length
    for(i=0;i<len;i++)
    {
    if(document.dropdown.droptest.options[i].value==optval)
    {document.dropdown.droptest.options[i].selected=true}
    }
    }
    </SCRIPT>
    </head>
    <body onLoad="setsel()">
    <FORM NAME="dropdown">
    <SELECT NAME="droptest">
    <OPTION VALUE="Zero">Zero</OPTION>
    <OPTION VALUE="One">One</OPTION>
    <OPTION VALUE="Two">Two</OPTION>
    </SELECT>
    </body>
    </html>

  3. #3
    SitePoint Addict jamesbond's Avatar
    Join Date
    Feb 2001
    Location
    The Netherlands
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks requestcode!

    Your code works great!


Bookmarks

Posting Permissions

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