...

View Full Version : :: automatically making all select boxes go to default value ::



babelfish
08-20-2002, 11:36 AM
can i do this as the page loads?

<form name="est_projects">
<select class="projectsmenus" name="FeaturedProjects" onChange="javascript:var projecturl=this.options[this.selectedIndex].value;
document.location.href = projecturl;return true;">
<option selected>:: Featured projects ::</option>
<option value="http://www.google.com">Boston, Queen St</option>
<option value="http://www.hotmail.com">Brighton, North Street</option>
<option value="http://www.lycos.com">Darlington</option>
<option value="http://www.excite.co.uk">Ely, Town Centre</option>
</select>
</form>

im using that so far but if the user presses the back button all the menus lose their titles (the selected one)

i have 3 of these on the page - any ideas?

x_goose_x
08-20-2002, 02:36 PM
<body onload="document.est_projects.FeaturedProjects.options[0].selected=true;">
<form name="est_projects">
<select class="projectsmenus" name="FeaturedProjects" onChange="javascript: var projecturl=this.options[this.selectedIndex].value; document.location.href = projecturl;return true;">
<option selected>:: Featured projects ::</option>
<option value="http://www.google.com">Boston, Queen St</option>
<option value="http://www.hotmail.com">Brighton, North Street</option>
<option value="http://www.lycos.com">Darlington</option>
<option value="http://www.excite.co.uk">Ely, Town Centre</option>
</select>
</form>
</body>

babelfish
08-20-2002, 04:14 PM
thanks m8 - works a treat :)

beetle
08-20-2002, 07:19 PM
You could also do something like this
function setSelects() {
var selects = document.getElementsByTagName('SELECT');
for (var i in selects)
selects[i].selectedIndex = 0;
}

<body onLoad="setSelects();"> also, your onChange event looks far too complicated. Why not this?
onChange="top.location.href=this.options[this.selectedIndex].value;"

adios
08-20-2002, 11:25 PM
None of those restore the default option ("selected"); they simply set the first as selected. This works for single selects:

<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">

function reset_selects() {
var el, f = 0, e;
while (frm = document.forms[f++]) {
e = 0;
while (el = frm.elements[e++]) {
if (el.type == 'select-one') {
for (var o=0; el.options[o]; ++o)
if (el.options[o].defaultSelected) {
el.selectedIndex = o;
break;
}
if (o == el.options.length) el.selectedIndex = 0;
}
}
}
}

onload = reset_selects;

</script>
</head>
<body>
<form>
<select>
<option>no default option</option>
<option>option</option>
<option>option</option>
</select>
<select>
<option selected="selected">default option</option>
<option>option</option>
<option>option</option>
</select>
<select>
<option>option</option>
<option selected="selected">default option</option>
<option>option</option>
</select>
<select>
<option>option</option>
<option>option</option>
<option selected="selected">default option</option>
</select>
</form>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum