question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

problem when using jquery.select2 in a popup

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
JamoCAcommented, 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 ) {
1reaction
swapnil-spyryoxcommented, Jun 16, 2018

Simply add following code

$(function () { $(‘.class-name’).magnificPopup({ callbacks: { open: function(){ $(‘.mfp-wrap’).css(“overflow”, “initial”) $(‘.mfp-wrap’).removeAttr(‘tabindex’) }, }, }); });

Read more comments on GitHub >

github_iconTop Results From Across the Web

Common problems - The jQuery replacement for select boxes
Select2 does not function properly when I use it inside a Bootstrap modal. ... This issue occurs because Bootstrap modals tend to steal...
Read more >
Select2 doesn't work when embedded in a bootstrap modal
According to the official select2 documentation this issue occurs because Bootstrap modals tend to steal focus from other elements outside of the modal....
Read more >
Select2 search input not working in Bootstrap Modal · Issue #41
You can fix this by removing the tabindex attribute from the .modal . This will allow moving around in the Select2 box, but...
Read more >
Bootstrap modal form validation not working with select2
I am using bootstrap modal form calling a partialview from Index.cshtml. It's work fine until I use Select2. The same partialview working ...
Read more >
Select2 and Bootstrap Integration by Keenthemes
Select2's is globally initialized through data-control="select2" HTML attribute as defined in src/js/layout/app.js and some options can be be passed through ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found