Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Chosen Plugin : Capturing focus to open when tabbing

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

like image 208
SeanCocteau Avatar asked Jan 24 '13 12:01

SeanCocteau


People also ask

How do I focus an element on a tab?

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".

What is the focus event in jQuery?

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.

What is the use of the focus method in HTML?

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> ).

Why can't I set focus to a hidden element?

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:


1 Answers

  1. 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

  2. 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)

  3. 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');

like image 144
paulitto Avatar answered Sep 30 '22 13:09

paulitto