简体   繁体   中英

focus() not working for a select box(dropdown)

On some particular event i want to focus on a particular select box. But

document.getElementById('myselect').focus()

is not working. It is working fine for a text input but not for a select box.

Are you sure that it isn't focusing?

I just ran a quick test on jsFiddle with the following code - jsFiddle :

<select name="dropdown" id="dropdown">
    <option value="1">One</option>
    <option value="2">two</option>
</select>

<input type="text" id="textfield" /><br />

    <button id="clickme" 
         onclick="javascript: document.getElementById('dropdown').focus()">
            focus dropdown</button>
    <button id="clickme1" 
         onclick="javascript: document.getElementById('textfield').focus()">
        focus textfield</button>

 Pressing the "Focus Dropdown" button does set the focus to the drop down field, but it doesn't actually appear to do anything.  The dropdown field isn't highlighted, although if I press the arrow keys, different values will be selected.

it should work and its working.. check the fiddle

--html--

<select id="dpk">
    <option> first </option>
    <option> second </option>
    <option> third </option>
</select>
<input type="button" value="focus Dropdown" id="btn" onclick="my_method()" />

--javscript---

function my_method()
{
    document.getElementById('dpk').focus();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM