hgarciap hgarciap - 4 months ago 12
Javascript Question

focus a select on any option selected from other select

I have 2 selects and what I need is to focus the 2nd select when selecting any option from the 1st select. My code is bellow as a sinpet code but it only set focus the 2nd select when a value is a number.



var acti = document.getElementById('activity');
var tip = document.getElementById('type');

activity.onchange = function () {
switch (this.value) {
case '1':
type.focus();
break;
}
};

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<select id="activity">
<option value="" selected="">choose one</option>
<option value="1">Act 1</option>
<option value="2">Act 2</option>
<option value="3">Act 3</option>
<option value="4">Act 4</option>
</select>
<select id="type">
<option value="">choose one</option>
</select>




Answer

I think this would work:

$('#activity').change(function(){ $('#type').focus() })

$('#activity').change(function(){
    $('#type').focus()
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<select id="activity">
    <option value="" selected="">choose one</option>
    <option value="1">Act 1</option>
    <option value="2">Act 2</option>
    <option value="3">Act 3</option>
    <option value="4">Act 4</option>
</select>
<select id="type">
    <option value="">choose one</option>
</select>