Linked DropdownsPart 2 : Get the Script |
|
Join the DiscussionMore of this FeatureThe first thing you need to do to link two dropdowns together like this is to write the HTML for the dropdown lists. Here is the code that I used for the example (note the code in bold which is needed to link in the Javascript.
<form action="#">
Real: <select id="actual"> <option value=" " selected="selected"></option> <option value="A">Option One</option> <option value="B">Option Two</option> <option value="C">Option Three</option> <option value="D">Option Four</option> </select> Estimate: <select onchange="listSel(this,'actual')"> <option value=" " selected="selected"></option> <option value="A">Option One</option> <option value="B">Option Two</option> <option value="C">Option Three</option> <option value="D">Option Four</option> </select> </form> The Javascript code to link these two together is fairly straightforward as well.
function listSel(fld,id) {
var opt = fld.selectedIndex; if (fld[opt].value != ' ') { var sel = document.getElementById(id); for (var i = sel.options.length -1; i > -1; i--) { if (fld[opt].value == sel[i].value) sel[i].selected = true; } } } To apply that exact link between two of your dropdowns simply save the Javascript as shown to a file (eg. droplist.js) and then link it into the head of your web page like this:
<script type="text/javascript"
src="droplist.js">
</script> If you don't want to exclude blank values from the link then remove the first if statement line along with the corresponding end } on the second last line. If instead you want to exclude additional values from the link simply modify that if statement to test for all of the values to be excluded. Any values that exist in the second list and not in the first will be automatically excluded (for obvious reasons) without your needing to add code for them.
Introduction | The Script
|

