We've all seen sites that feature drop down lists that automatically take you to your selection without you needing to make the cumbersome and lengthy step of hitting a "Go" button. If you've ever wondered how they do this then as you probably expected it's very simple.
What on Earth am I talking about?
Picture a drop down list as follows:
We often see a button next to the list that the user should press in order to actually activate there selection:
How do we do it?
A typical dropdown list is instantiated using the following:
(Give or take some attributes). What we do is add an attribute that instructs the page to call our handler for when the list selection is changed.
<select name=select1 onchange='OnChange(this.form.select1);'>
Our handler will look up the value that has just been selected, and navigate to an appropriate URL. An example of this is shown below:
var myindex = dropdown.selectedIndex
var SelValue = dropdown.options[myindex].value
var baseURL = <Some value based on SelValue>
top.location.href = baseURL;
We use the
</select><noscript><INPUT type="submit" value="Go" name=submit1></noscript>
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.
His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.
He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.
Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.