Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript: form select option and input field

I have an form input field, when a user types "text 2", I want that "text 2" selected in the form select:

<select id="formsel">
    <option value="text 1">text 1</option>
    <option value="text 3">text 2</option>
    <option value="text 3">text 3</option>
</select>
<input type='text' id='input' />

I get the value from the input like this:

var input_val = document.getElementById('input').value; 

But I can not select the option from the dynamic form select with

document.form.formsel.value = input_val;

Can anyone see what I'm doing wrong?

like image 237
JasperM Avatar asked Oct 25 '25 02:10

JasperM


1 Answers

Your code doesn't show the form you are trying to access with document.form, so I'm assuming there is no form. Try accessing the select by its id. This seems to work for me:

<script>
document.getElementById('input').onkeyup = function()
{
    var input_val = document.getElementById('input').value;
    document.getElementById('formsel').value = input_val;
}
</script>
like image 60
Armon Avatar answered Oct 26 '25 22:10

Armon