Select2 open dropdown on focus text input
12,216
They have explained with example for how to open drop-down option.
Documentation URL: https://select2.github.io/examples.html and please check Programmatic access
section.
HTML Part:
<select id="e1">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<br>
<br>
<input type="text" id="nameID" name="firstname">
jQuery Part:
$("#e1").select2();
// With change event
$("input").on("change", function () {
$("#e1").select2("open");
});
// With focus event
$( "input" ).focus(function() {
$("#e1").select2("open");
});
Try this example with onChange event: http://jsfiddle.net/mananpatel/73wypp1w/5/
Let me know if you need help :)
![Saeide](https://i.stack.imgur.com/G4QpL.jpg?s=256&g=1)
Author by
Saeide
Updated on August 21, 2022Comments
-
Saeide almost 2 years
I have a form with one text input and one select2 element (Version 4.0.0). I want when focus on text input, the select2 open.
Here’s what I’ve tried :
<select list="userAccountsMap.values" listKey="accountNo" listValue="accountNo"/> <input id="destinationAccount" type="text" name="destinationAccount"> <script type="text/javascript"> ('select').each(function( index ) { $(this).select2(); } }); $("#destinationAccount").focus(function () { $("select").trigger('select2:open'); }); </script>
But it is not work. See fiddle here: http://jsfiddle.net/Hatamikhah/73wypp1w/1/
I don't understand what my problem is.