Page 1 of 1

Make my drop box hop to an option :-(

Posted: Wed Dec 01, 2004 5:29 pm
by Chris Corbyn
Ok, I'm making a form update dynamically and need educating :-)

Suppose I wanted a drop down box to automatically change it's selection depending on what a user types into a text box. The drop box hops to the option which has a value equal to the string keyed in the text box. I'm trying with no luck :-(

Here's my attempted method....

Code: Select all

<script language="javascript">
<!--
function updateDrop(el1,el2) &#123;
    for (i=0; i<el2.options.length; i++) &#123;
        if (el1.value == el2.options&#1111;i].value) &#123;
            el2.options&#1111;i].selected == true
        &#125;
    &#125;
&#125;
// -->
</script>
</head>
<body>
<form action="demo" method="post" name="demo">
<input type="text" id="text1" name="text1" onKeyup="updateDrop(this,document.getElementById('drop1'))">
<br>
<select name="drop1" id="drop1">
    <option value="12345">Numbers 1 to 5</option>
    <option value="6789" selected>Numbers 6 to 9</option>
    <option value="7529">Random 7,5,2&9</option>
</select>
</form>
So the form loads with the second option selected but if the user types the string "12345" in the text box I'd want to see my drop box hop to the first option.

My method doesn't seem to work. Any better ways to do this?

Thanks in advance :-)

Posted: Wed Dec 01, 2004 6:07 pm
by Chris Corbyn
Solved this one. Just needed to assign the selected index to the relevant integer key