Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem when using jquery.select2 in a popup #280

Closed
clime opened this issue Nov 14, 2013 · 9 comments
Closed

problem when using jquery.select2 in a popup #280

clime opened this issue Nov 14, 2013 · 9 comments

Comments

@clime
Copy link

clime commented Nov 14, 2013

Hey,

when using jquery.select2 inside a popup, you can't type into the select2's searchbox. There seems to be some kind of focus/blur loop. I have described the issue in more detail here: http://stackoverflow.com/questions/19990191/select2-in-magnific-popup-search-input-cannot-be-focused. Here is jsfiddle that shows the problem: http://jsfiddle.net/clime/qweWa/15/. I have tracked the problem to the following code in magnific popup:

        setTimeout(function() {

            if(mfp.content) {
                mfp._addClassToMFP(READY_CLASS);
                _setFocus();
            } else {
                // if content is not defined (not loaded e.t.c) we add class only for BG
                mfp.bgOverlay.addClass(READY_CLASS);
            }

            // Trap the focus in popup
            _document.on('focusin' + EVENT_NS, function (e) {
                if( e.target !== mfp.wrap[0] && !$.contains(mfp.wrap[0], e.target) ) {
                    _setFocus();
                    return false;
                }
            });

        }, 16);

_setFocus is called indefinietly when select2 is clicked on in a popup.

@dimsemenov
Copy link
Owner

Ok, so the problem is that select2 "dropdown" input field is added directly to the body, instead of inside current select2 DOM element. Which makes popup think that it's outside of it.

magnific popup responsive jquery lightbox plugin 2013-11-15 11-04-55

When tab focus changes, the popup checks if the new focused element is inside the popup. If it is outside, popup sets focus back to the root element of popup. This is done to "lock" the tab focus in popup.

I don't think that disabling such feature or removing "focus" from select2 is a good idea, so I'd recommend to override _onFocusIn event handler and add an additional check:

$.magnificPopup.instance._onFocusIn = function(e) {
          // Do nothing if target element is select2 input
          if( $(e.target).hasClass('select2-input') ) {
            return true;
          } 
          // Else call parent method
          $.magnificPopup.proto._onFocusIn.call(this,e);
};

The updated fiddle: http://jsfiddle.net/qweWa/21/

Important note, I've added function _onFocusIn in 0.9.9 update, so make sure that latest version of popup JS file is used. https://rawgithub.com/dimsemenov/Magnific-Popup/master/dist/jquery.magnific-popup.js

@dimsemenov
Copy link
Owner

If you'll find any issues let me know...

@clime
Copy link
Author

clime commented Nov 15, 2013

It works well. Thanks.

@nestordeharo
Copy link

Great!...that solution solves my painful problem...

@s0w4
Copy link

s0w4 commented Jul 23, 2014

Небольшая опечатка:
image

@JamoCA
Copy link

JamoCA commented Jul 21, 2016

The sample code should be updated for the new Select2 v4.x className.

$.magnificPopup.instance._onFocusIn = function(e) {
    // Do nothing if target element is select2 input
    if( $(e.target).hasClass('select2-search__field') ) {
        return true;
    } 
    // Else call parent method
    $.magnificPopup.proto._onFocusIn.call(this,e);
};

A more future-proof solution may involve searching for "select2" anywhere in the class attribute.

if( $(e.target).attr('class').indexOf('select2') >= 0 ) {

@ketancmarix
Copy link

It works. Thanks

@swapnil-spyryox
Copy link

Simply add following code

$(function () {
$('.class-name').magnificPopup({
callbacks: {
open: function(){
$('.mfp-wrap').css("overflow", "initial")
$('.mfp-wrap').removeAttr('tabindex')
},
},
});
});

@chachapara
Copy link

If you'll find any issues let me know...

bro in popup video in video link is not working when open the link video is not play

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants