Make my drop box hop to an option :-(

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Make my drop box hop to an option :-(

Post 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 :-)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Solved this one. Just needed to assign the selected index to the relevant integer key
Post Reply