PDA

View Full Version : How to make the select first option in forms to be selected after refresh?



Oalddesign
04-28-2011, 12:51 PM
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.

molendijk
04-28-2011, 02:52 PM
<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.

Oalddesign
04-28-2011, 03:47 PM
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

molendijk
04-28-2011, 06:06 PM
This will work:

<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.

Oalddesign
04-28-2011, 08:05 PM
Wow working great thanks a lot for you help molendijk