Selecting a Specific Option

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

    Selecting a Specific Option

    I need to be able to select a specific option in a select list. This is
    what I have:
    <select name="mysel">
    <option value=""></option>
    <option value="1">First </option>
    <option value="2" selected>First</option>
    <option value="3">First </option>
    </select>

    But I want to select the first option. Here is my code:

    var frm_objs = document.report .elements;
    for ( j=0; j<frm_objs.leng th; j++ )
    {
    if ( frm_objs[j].type=='select-one' )
    {
    if ( frm_objs[j].name=='mysel' )
    {
    frm_objs[j].options[0].selected=true;
    }
    }
    }

    I think is should select the first ooption but it does not. The default
    option 2 remains selected.

    Any help is apreciated.

  • RobB

    #2
    Re: Selecting a Specific Option

    mike wrote:[color=blue]
    > I need to be able to select a specific option in a select list. This[/color]
    is[color=blue]
    > what I have:
    > <select name="mysel">
    > <option value=""></option>
    > <option value="1">First </option>
    > <option value="2" selected>First</option>
    > <option value="3">First </option>
    > </select>
    >
    > But I want to select the first option. Here is my code:
    >
    > var frm_objs = document.report .elements;
    > for ( j=0; j<frm_objs.leng th; j++ )
    > {
    > if ( frm_objs[j].type=='select-one' )
    > {
    > if ( frm_objs[j].name=='mysel' )
    > {
    > frm_objs[j].options[0].selected=true;
    > }
    > }
    > }
    >
    > I think is should select the first ooption but it does not. The[/color]
    default[color=blue]
    > option 2 remains selected.
    >
    > Any help is apreciated.[/color]

    <html>
    <head>
    <script type="text/javascript">

    window.onload = function()
    {
    var frm_objs = document.report .elements;
    for ( j=0, len = frm_objs.length ; j<len; j++ )
    {
    obj = frm_objs[j];
    if ( obj.type=='sele ct-one' //unnecessary
    && obj.name=='myse l' )
    {
    obj.options[0].selected=true;
    }
    }
    }

    </script>
    </head>
    <body>
    <form name="report">
    <select name="mysel">
    <option value="0">Optio n 0</option>
    <option value="1">Optio n 1</option>
    <option value="2" selected>Option 2</option>
    <option value="3">Optio n 3</option>
    </select>
    </form>
    </body>
    </html>

    Comment

    • mscir

      #3
      Re: Selecting a Specific Option

      mike wrote:
      [color=blue]
      > I need to be able to select a specific option in a select list. This is
      > what I have:[/color]
      <snip>

      <head>
      <script type="text/javascript">
      function setselectedinde x(n) {
      var m=document.getE lementById('myl ist');
      m.selectedIndex = n;
      }
      </script>
      </head>

      <body>
      <select id='mylist'>
      <option value=0>Zero</li>
      <option value=1>One</li>
      <option value=2>Two</li>
      </select>
      <br><br>
      <input type='button' value='Set SelectedIndex to 0'
      onclick="setsel ectedindex(0)"; ><br>
      <input type='button' value='Set SelectedIndex to 1'
      onclick="setsel ectedindex(1)"; ><br>
      <input type='button' value='Set SelectedIndex to 2'
      onclick="setsel ectedindex(2)"; ><br>
      </body>

      Comment

      Working...