Results 1 to 5 of 5

Thread: How to make the select first option in forms to be selected after refresh?

  1. #1
    Join Date
    Apr 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How to make the select first option in forms to be selected after refresh?

    Hey, i have a form where i use select options i have few options like 1,2,3,4
    now if i change the option from 1 to 3 and then hit refresh it stay as 3 and not going back to 1 (that happen in firefox, on some other browsers you won't see the problem).
    Wanted to know if there is any way to make it go to the first option after refreshing?
    I mean if when i enter the page it is on 1 and then i chose 3 and refresh it will go back to 1 instead of stay as 0.
    I think this is something that can be done by javascript and would be great to know what the solution for this.

  2. #2
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,865
    Thanks
    49
    Thanked 260 Times in 252 Posts
    Blog Entries
    56

    Default

    Code:
    <body onunload="document.getElementById('some_select').selectedIndex=0">
    <select id="some_select">
    <option>one</option>
    <option>two</option>
    <option>three</option>
    <option>four</option>
    </select>
    Arie Molendijk.

  3. #3
    Join Date
    Apr 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Hey thanks for your replay its worked but the problem is that it working only for one select if i am trying to put 2 of them it works onle for on of the selects so that mean it work only on one select that use the div and not more then that. i could create a different div id and java for each one but that would probably take some time.
    So wanted to know if there is other way to do that to all the selects that using the div

  4. #4
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,865
    Thanks
    49
    Thanked 260 Times in 252 Posts
    Blog Entries
    56

    Default

    This will work:
    Code:
    <head>
    <script type="text/javascript">
    function select_first_option()
    {
    sel = document.getElementsByTagName('select')
    for (var i = 0; i < sel.length; i++) 
    {sel[i].selectedIndex=0;}
    }
    </script>
    </head>
    
    <body onunload="select_first_option()"
    
    <select>
    <option>one</option>
    <option>two</option>
    <option>three</option>
    <option>four</option>
    </select>&nbsp;&nbsp;
    
    <select>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    </select>&nbsp;&nbsp;
    
    <select>
    <option>een</option>
    <option>twee</option>
    <option>drie</option>
    <option>vier</option>
    </select>
    
    </body>
    Arie.

  5. #5
    Join Date
    Apr 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Wow working great thanks a lot for you help molendijk

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
  •