We're currently using the Chosen Dropdown Plugin which is rather awesome, apart from one minor issue. When we're using a single dropdown, if you tab into the 'chosen' control, the actual dropdown portion is not shown. However, when applying the plugin to a multiple 'select', it does appear.
Having been through the documentation and GitHub issues, there seems to be a lot of mentions regarding tab ordering and focusing, but nothing that seemingly deals with this rather simple requirement; Display the dropdown when receiving focus when tabbing.
So assuming that this functionality is not part of the plugin, is there an alternative such as capturing the focus of the anchor tag?
$('.chzn-single').focus(function(e){
alert('I should be focused!')
});
So far, I haven't been successful and was wondering whether any others have experienced this issue. You can check out this jsfiddle that demonstrates the problem
To focus an element, press the Tab key or call the element's focus () method. Remove an element from the tab order # Remove an element using tabindex="-1".
The focus event occurs when an element gets focus (when selected by a mouse click or by "tab-navigating" to it). The focus() method triggers the focus event, or attaches a function to run when a focus event occurs.
This method is a shortcut for .on ( "focus", handler ) in the first and second variations, and .trigger ( "focus" ) in the third. The focus event is sent to an element when it gains focus. This event is implicitly applicable to a limited set of elements, such as form elements ( <input>, <select>, etc.) and links ( <a href> ).
Attempting to set focus to a hidden element causes an error in Internet Explorer. Take care to only use .focus () on elements that are visible. To run an element's focus event handlers without setting focus to the element, use .triggerHandler ( "focus" ) instead of .focus (). For example, consider the HTML:
You should keep track of focus event for the search input thats inside chosen conainer, not the anchor element, then trigger mousedown event for the chosen container - that's the event that it listens to when opening a dropdown
You need to use delegated events approach to bind event handler to elements added dynamically (for jquery 1.7.1 and earlier you may just use 'live' method)
You also need to check if the container is active currently, to avoid recursive calls (when chosen dropdown gets opened - search input will be focused again)
$('body').on('focus', '.chzn-container-single input', function() { if (!$(this).closest('.chzn-container').hasClass('chzn-container-active')) $(this).closest('.chzn-container').trigger('mousedown'); //or use this instead //$('#select').trigger('liszt:open'); });
Here's working jsfiddle
Instead of $(this).closest('.chzn-container').trigger('mousedown');
you may also trigger plugin's internal event: $('#select').trigger('liszt:open');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With