Select2 doesn't work when embedded in a bootstrap modal












272














When I use a select2 (input) in bootstrap modal, I can't type anything into it. It's like disabled? Outside he modal select2 works fine.



enter image description here



working example: http://jsfiddle.net/byJy8/1/
code:



<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Panel</h3>
</div>
<div class="modal-body" style="max-height: 800px">


<form class="form-horizontal">

<!-- Text input-->
<div class="control-group">
<label class="control-label" for="vdn_number">Numer</label>
<div class="controls">
<!-- seleect2 -->
<input name="vdn_number" type="hidden" id="vdn_number" class="input-large" required="" />
</div>
</div>

</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button class="btn btn-primary">Save changes</button>
</div>
</div>


adn js



$("#vdn_number").select2({
placeholder: "00000",
minimumInputLength: 2,
ajax: {
url: "getAjaxData/",
dataType: 'json',
type: "POST",
data: function (term, page) {
return {
q: term, // search term
col: 'vdn'
};
},
results: function (data) { // parse the results into the format expected by Select2.
// since we are using custom formatting functions we do not need to alter remote JSON data
return {results: data};
}
}
});


answers:



here you can find a quick fix



and here is 'the right way': Select2 doesn't work when embedded in a bootstrap modal










share|improve this question
























  • show your code ?
    – Tushar Gupta
    Aug 28 '13 at 11:51










  • @TusharGupta updated
    – breq
    Aug 28 '13 at 12:03










  • That is, removing the attribute tabindex="-1" from the element.
    – Nikit Barochiya
    Aug 3 '17 at 7:00










  • dboswell's answer is the right solution, BUT, the dropdownParent should not be on the root div, but deeper, for example the div with class modal-content, otherwise the dropdown positions get messed up when scrolling the modal.
    – Shahin Dohan
    Nov 4 at 1:44
















272














When I use a select2 (input) in bootstrap modal, I can't type anything into it. It's like disabled? Outside he modal select2 works fine.



enter image description here



working example: http://jsfiddle.net/byJy8/1/
code:



<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Panel</h3>
</div>
<div class="modal-body" style="max-height: 800px">


<form class="form-horizontal">

<!-- Text input-->
<div class="control-group">
<label class="control-label" for="vdn_number">Numer</label>
<div class="controls">
<!-- seleect2 -->
<input name="vdn_number" type="hidden" id="vdn_number" class="input-large" required="" />
</div>
</div>

</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button class="btn btn-primary">Save changes</button>
</div>
</div>


adn js



$("#vdn_number").select2({
placeholder: "00000",
minimumInputLength: 2,
ajax: {
url: "getAjaxData/",
dataType: 'json',
type: "POST",
data: function (term, page) {
return {
q: term, // search term
col: 'vdn'
};
},
results: function (data) { // parse the results into the format expected by Select2.
// since we are using custom formatting functions we do not need to alter remote JSON data
return {results: data};
}
}
});


answers:



here you can find a quick fix



and here is 'the right way': Select2 doesn't work when embedded in a bootstrap modal










share|improve this question
























  • show your code ?
    – Tushar Gupta
    Aug 28 '13 at 11:51










  • @TusharGupta updated
    – breq
    Aug 28 '13 at 12:03










  • That is, removing the attribute tabindex="-1" from the element.
    – Nikit Barochiya
    Aug 3 '17 at 7:00










  • dboswell's answer is the right solution, BUT, the dropdownParent should not be on the root div, but deeper, for example the div with class modal-content, otherwise the dropdown positions get messed up when scrolling the modal.
    – Shahin Dohan
    Nov 4 at 1:44














272












272








272


69





When I use a select2 (input) in bootstrap modal, I can't type anything into it. It's like disabled? Outside he modal select2 works fine.



enter image description here



working example: http://jsfiddle.net/byJy8/1/
code:



<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Panel</h3>
</div>
<div class="modal-body" style="max-height: 800px">


<form class="form-horizontal">

<!-- Text input-->
<div class="control-group">
<label class="control-label" for="vdn_number">Numer</label>
<div class="controls">
<!-- seleect2 -->
<input name="vdn_number" type="hidden" id="vdn_number" class="input-large" required="" />
</div>
</div>

</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button class="btn btn-primary">Save changes</button>
</div>
</div>


adn js



$("#vdn_number").select2({
placeholder: "00000",
minimumInputLength: 2,
ajax: {
url: "getAjaxData/",
dataType: 'json',
type: "POST",
data: function (term, page) {
return {
q: term, // search term
col: 'vdn'
};
},
results: function (data) { // parse the results into the format expected by Select2.
// since we are using custom formatting functions we do not need to alter remote JSON data
return {results: data};
}
}
});


answers:



here you can find a quick fix



and here is 'the right way': Select2 doesn't work when embedded in a bootstrap modal










share|improve this question















When I use a select2 (input) in bootstrap modal, I can't type anything into it. It's like disabled? Outside he modal select2 works fine.



enter image description here



working example: http://jsfiddle.net/byJy8/1/
code:



<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Panel</h3>
</div>
<div class="modal-body" style="max-height: 800px">


<form class="form-horizontal">

<!-- Text input-->
<div class="control-group">
<label class="control-label" for="vdn_number">Numer</label>
<div class="controls">
<!-- seleect2 -->
<input name="vdn_number" type="hidden" id="vdn_number" class="input-large" required="" />
</div>
</div>

</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button class="btn btn-primary">Save changes</button>
</div>
</div>


adn js



$("#vdn_number").select2({
placeholder: "00000",
minimumInputLength: 2,
ajax: {
url: "getAjaxData/",
dataType: 'json',
type: "POST",
data: function (term, page) {
return {
q: term, // search term
col: 'vdn'
};
},
results: function (data) { // parse the results into the format expected by Select2.
// since we are using custom formatting functions we do not need to alter remote JSON data
return {results: data};
}
}
});


answers:



here you can find a quick fix



and here is 'the right way': Select2 doesn't work when embedded in a bootstrap modal







jquery twitter-bootstrap jquery-select2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 23 '17 at 12:10









Community

11




11










asked Aug 28 '13 at 11:49









breq

12.9k154481




12.9k154481












  • show your code ?
    – Tushar Gupta
    Aug 28 '13 at 11:51










  • @TusharGupta updated
    – breq
    Aug 28 '13 at 12:03










  • That is, removing the attribute tabindex="-1" from the element.
    – Nikit Barochiya
    Aug 3 '17 at 7:00










  • dboswell's answer is the right solution, BUT, the dropdownParent should not be on the root div, but deeper, for example the div with class modal-content, otherwise the dropdown positions get messed up when scrolling the modal.
    – Shahin Dohan
    Nov 4 at 1:44


















  • show your code ?
    – Tushar Gupta
    Aug 28 '13 at 11:51










  • @TusharGupta updated
    – breq
    Aug 28 '13 at 12:03










  • That is, removing the attribute tabindex="-1" from the element.
    – Nikit Barochiya
    Aug 3 '17 at 7:00










  • dboswell's answer is the right solution, BUT, the dropdownParent should not be on the root div, but deeper, for example the div with class modal-content, otherwise the dropdown positions get messed up when scrolling the modal.
    – Shahin Dohan
    Nov 4 at 1:44
















show your code ?
– Tushar Gupta
Aug 28 '13 at 11:51




show your code ?
– Tushar Gupta
Aug 28 '13 at 11:51












@TusharGupta updated
– breq
Aug 28 '13 at 12:03




@TusharGupta updated
– breq
Aug 28 '13 at 12:03












That is, removing the attribute tabindex="-1" from the element.
– Nikit Barochiya
Aug 3 '17 at 7:00




That is, removing the attribute tabindex="-1" from the element.
– Nikit Barochiya
Aug 3 '17 at 7:00












dboswell's answer is the right solution, BUT, the dropdownParent should not be on the root div, but deeper, for example the div with class modal-content, otherwise the dropdown positions get messed up when scrolling the modal.
– Shahin Dohan
Nov 4 at 1:44




dboswell's answer is the right solution, BUT, the dropdownParent should not be on the root div, but deeper, for example the div with class modal-content, otherwise the dropdown positions get messed up when scrolling the modal.
– Shahin Dohan
Nov 4 at 1:44












21 Answers
21






active

oldest

votes


















508














Ok, I've got it to work.



change



<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Panel</h3>
</div>
<div class="modal-body" style="max-height: 800px">


to



<div id="myModal" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Panel</h3>
</div>
<div class="modal-body" style="max-height: 800px">


(remove tabindex="-1" from modal)






share|improve this answer



















  • 18




    May be there another solution? Because modal window then won't close by Esc key.
    – woto
    Oct 20 '13 at 15:47






  • 18




    removing tabindex="-1" didn't work form me. I've just changed <div class="modal-body"> to <div class="modal-body" style="overflow:hidden;">
    – Wissem
    Nov 27 '13 at 8:49






  • 28




    How the hell you discovered that ?
    – DontVoteMeDown
    Oct 1 '14 at 20:03






  • 6




    I have tried all options , but nothing works for me. I am using select2 4.0
    – Girish Gupta
    Sep 4 '15 at 4:19






  • 4




    Thanks! It's been 4 years and it's still a valid fix.
    – Linek
    Jul 21 '17 at 12:40



















182














For Select2 v4:



Use dropdownParent to attach the dropdown to the modal dialog, rather than the HTML body.



<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
<select id="select2insidemodal" multiple="multiple">
<option value="AL">Alabama</option>
...
<option value="WY">Wyoming</option>
</select>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>


<script>

$(document).ready(function() {
$("#select2insidemodal").select2({
dropdownParent: $("#myModal")
});
});

</script>


This will attach the Select2 dropdown so it falls within the DOM of the modal rather than to the HTML body (the default). See https://select2.org/dropdown#dropdown-placement






share|improve this answer



















  • 8




    Confirmed, this is working like a charm and seems to be the best solution to this problem!
    – lacco
    Dec 7 '15 at 17:30






  • 25




    This worked for me (with v4.0.2), but I had to use dropdownParent: $("#myModal"), not dropdownParent: "#myModal".
    – Saxon Druce
    Apr 12 '16 at 7:58






  • 4




    @SaxonDruce This worked! Thanks for the extra info.
    – Rohmer
    May 7 '16 at 3:11






  • 2




    Thanks, this worked for me!
    – Luis de Haro
    Sep 2 '16 at 14:00






  • 3




    FWIW, in my case I needed to set the dropdownParent to the body of the modal, attaching it to the modal itself did not bring the z-index up high enough. e.g. dropdownParent: $('.modal-body', '#myModal')
    – DrewInTheMountains
    Dec 14 '17 at 18:03



















159














I found a solution to this on github for select2



https://github.com/ivaynberg/select2/issues/1436



For bootstrap 3, the solution is:



$.fn.modal.Constructor.prototype.enforceFocus = function() {};


Bootstrap 4 renamed the enforceFocus method to _enforceFocus, so you'll need to patch that instead:



$.fn.modal.Constructor.prototype._enforceFocus = function() {};


Explanation copied from link above:




Bootstrap registers a listener to the focusin event which checks whether the focused element is either the overlay itself or a descendent of it - if not it just refocuses on the overlay. With the select2 dropdown being attached to the body this effectively prevents you from entering anything into the the textfield.



You can quickfix this by overwriting the enforceFocus function which registers the event on the modal







share|improve this answer



















  • 1




    @pymarco That one should have been there a little earlier to be picked as the answer! Thanks man, for providing a real answer to a really annoying problem.
    – ilter
    Dec 30 '13 at 23:44








  • 2




    @pymarco This is the correct solution. Thank you for saving my time.
    – VKPRO
    Jan 12 '15 at 8:57






  • 2




    You saved me loads of time with an issue that didn't always rear it's head. Thank you!
    – user3036342
    May 18 '15 at 13:47






  • 1




    This worked for me!! I was browsing and browsing through the issues threads for select2 in github and didn't run into that post you provided. It was googling and finding this page in stackoverflow what saved my life. Thank you!
    – luis.ap.uyen
    Jan 19 '16 at 9:27






  • 2




    where do I put this code and what function should it address? Sorry, JS noob here, I tried making this a part of the select2 JS or placing it at top / end of the page, all without success. $.fn.modal.Constructor.prototype.enforceFocus = $.noop;
    – Armitage2k
    Nov 27 '16 at 0:59



















29














Just remove tabindex="-1" and add style overflow:hidden



Here is an example:



<div id="myModal" class="modal fade" role="dialog" style="overflow:hidden;">
<!---content modal here -->
</div>





share|improve this answer























  • This one worked for me! (Firefox & Chrome)
    – Julian Koster
    Jul 31 '17 at 14:32












  • it works but when I have long modal I can't scroll down !
    – Tarek Kalaji
    Mar 17 at 16:40






  • 1




    @TarekKalaji did you ever figure out how to allow scrolling with this solution?
    – Cameron Taylor
    Mar 28 at 13:20










  • Tried removing the tabindex but was not enought. Adding overflow: hidden is useful if the modal box is long...
    – Plasebo
    Apr 2 at 11:17










  • Worked for me removing the tab index attribute.
    – Glen
    Jun 4 at 11:25



















23














.select2-close-mask{
z-index: 2099;
}
.select2-dropdown{
z-index: 3051;
}


This is my solution with select2 4.0.0. Just override the css right below the select2.css import.
Please make sure the z-index is greater than your dialog or modal. I just add 2000 on the default ones. Cause my dialogs' z-index are about 1000.






share|improve this answer

















  • 1




    None of the solutions worked for select2 4.0.0 except this. Thanks a lot.
    – Neel
    Jun 26 '15 at 10:15










  • The same issue occurs if Select2 (v4.0.0) is in a popover. In bootstrap.css the z-index of .popover is set to 1060 so this fix works as well. I like it because it's simple and it solves the problem the same way the problem was created: z-index set in a style sheet.
    – Paul
    Jul 2 '15 at 18:02








  • 1




    The most trivial yet the only solution that worked. Thanks!
    – Ishtiaque Khan
    Oct 17 '15 at 1:23










  • I've added the above in my css file, but it has no changes. I'm using jQuery Mobile. Any suggestions please?
    – Sahil Khanna
    Feb 20 '16 at 7:03










  • @Sahil 1. make sure it's successfully overridden the original ones. simple way is add this after all the default css files2.check the z-index, ensure it's larger than the dialog's
    – Diluka W
    Feb 22 '16 at 8:55



















6














change select2.css file



z-index: 9998;
...
z-index: 9999;
...
z-index: 10000;


to



z-index: 10000;
...
z-index: 10001;
...
z-index: 10002;





share|improve this answer





















  • That's the only one works for me (jquery-ui-1.10.3.css, jquery-ui-1.9.1.js and select2 3.4.0). I have 2 jquery UI dialogs one on top of the other and select2 on the second one. Also I have put //allow interaction of select2, date and time picker on jQuery UI dialog $.ui.dialog.prototype._allowInteraction = function(e) { return !!$(e.target).closest('.ui-dialog, .ui-datepicker, .select2-drop').length; }; I have no idea if this help or not.
    – Adrian P.
    Feb 9 '16 at 17:29





















6














I had the same issue, updating z-index for .select2-container should do the trick. Make sure your modal's z-index is lower than select2's.



.select2-container {
z-index: 99999;
}


Updated:
In case above code doesn't work properly, also remove tabindexes from your modal as @breq suggested






share|improve this answer



















  • 1




    you need to implement this with @breq answer to make it works.
    – Ryan Arief
    Jun 11 '17 at 3:40










  • @RyanArief my bad, thanks.
    – hhk
    Jun 12 '17 at 7:44





















6














Jus use this code on Document.read()



$.fn.modal.Constructor.prototype.enforceFocus = function () {};





share|improve this answer

















  • 1




    The proper function name is "$(document).ready". So code should be like: $(document).ready(function () { $.fn.modal.Constructor.prototype.enforceFocus = function () { }; });
    – Lech Osiński
    Nov 2 '17 at 14:04








  • 1




    @LechOsiński - Thank you for actually describing how to use this little chunk of code. Your solution is the only one that worked for me. +1
    – Francis Bartkowiak
    Oct 18 at 0:50



















6














Answer that worked for me found here: https://github.com/select2/select2-bootstrap-theme/issues/41



$('select').select2({
dropdownParent: $('#my_amazing_modal')
});


Also doesn't require removing the tabindex.






share|improve this answer





















  • This is the nicest way, also keeps all functionality from modal.
    – Edofre
    Feb 21 at 14:05



















5














Just to understand better how tabindex elements works to complete accepted answer :




The tabindex global attribute is an integer indicating if the element can take input focus (is focusable), if it should participate to sequential keyboard navigation, and if so, at what position. It can take several values:

  -a negative value means that the element should be focusable, but should not be reachable via sequential keyboard navigation;


  -0 means that the element should be focusable and reachable via sequential keyboard navigation, but its relative order is defined by the platform convention;


  -a positive value means should be focusable and reachable via sequential keyboard navigation; its relative order is defined by the value of the attribute: the sequential follow the increasing number of the tabindex. If several elements share the same tabindex, their relative order follows their relative position in the document.




from : Mozilla Devlopper Network






share|improve this answer































    3














    Based on @pymarco answer I wrote this solution, it's not perfect but solves the select2 focus problem and maintain tab sequence working inside modal



        $.fn.modal.Constructor.prototype.enforceFocus = function () {
    $(document)
    .off('focusin.bs.modal') // guard against infinite focus loop
    .on('focusin.bs.modal', $.proxy(function (e) {
    if (this.$element[0] !== e.target && !this.$element.has(e.target).length && !$(e.target).closest('.select2-dropdown').length) {
    this.$element.trigger('focus')
    }
    }, this))
    }





    share|improve this answer





























      2














      If you use jquery mobile popup you must rewrite _handleDocumentFocusIn function:






      $.mobile.popup.prototype._handleDocumentFocusIn = function(e) {
      if ($(e.target).closest('.select2-dropdown').length) return true;
      }








      share|improve this answer





















      • I'm using jQuery Mobile and have embedded Select2 in a popup. The code you have provided works good on a PC browser. But when I run this code on a mobile browser, the popup closes and reopens when any value is selected. Can you suggest any changes I need to make?
        – Sahil Khanna
        Feb 20 '16 at 7:03



















      2














      If you have a problem with the iPad keyboard which hide the bootstrap modal while clicking on the select2 input, you can resolve this by adding the following rule after the initialization of the select2 input :



      if (navigator.userAgent.match(/iPhone|iPad|iPod/i)) {
      var styleEl = document.createElement('style'), styleSheet;
      document.head.appendChild(styleEl);
      styleSheet = styleEl.sheet;
      styleSheet.insertRule(".modal { position:absolute; bottom:auto; }", 0);
      document.body.scrollTop = 0; // Only for Safari
      }


      Taken from https://github.com/angular-ui/bootstrap/issues/1812#issuecomment-135119475



      EDIT: If your options are not shown properly, you need to use the dropdownParent attribute when initializing select2 :



      $(".select2").select2({
      dropdownParent: $("#YOURMODALID")
      });


      Good luck (:






      share|improve this answer































        2














        Okay, I know I'm late to the party. But let me share with you what worked for me.
        The tabindex and z-index solutions did not work for me.



        Setting the parent of the select element worked as per the common problems listed on select2 site.






        share|improve this answer





























          2














          This Problem Sloved Working For Me Single Jquery Function



          $('#myModal .select2').each(function() {  
          var $p = $(this).parent();
          $(this).select2({
          dropdownParent: $p
          });
          });





          share|improve this answer





























            1














            I have the same problem with the select2 in bootstrap modal, and the solution was to remove the overflow-y: auto; and overflow: hidden; from .modal-open and .modal classes



            Here is the example of using jQuery to remove the overflow-y:



            $('.modal').css('overflow-y','visible');
            $('.modal').css('overflow','visible');





            share|improve this answer





























              1














              i had this problem before , i am using yii2 and i solved it this way



              $.fn.modal.Constructor.prototype.enforceFocus = $.noop;





              share|improve this answer





























                1














                $("#IlceId").select2({
                allowClear: true,
                multiple: false,
                dropdownParent: $("#IlceId").parent(),
                escapeMarkup: function (m) {
                return m;
                },
                });


                this code is working. Thank you.






                share|improve this answer































                  1














                  I solved this generally in my project by overloading the select2-function. Now it will check if there is no dropdownParent and if the function is called on an element that has a parent of the type div.modal. If so, it will add that modal as the parent for the dropdown.



                  This way, you don't have to specify it every time you create a select2-input-box.



                  (function(){
                  var oldSelect2 = jQuery.fn.select2;
                  jQuery.fn.select2 = function() {
                  const modalParent = jQuery(this).parents('div.modal').first();
                  if (arguments.length === 0 && modalParent.length > 0) {
                  arguments = [{dropdownParent: modalParent}];
                  } else if (arguments.length === 1
                  && typeof arguments[0] === 'object'
                  && typeof arguments[0].dropdownParent === 'undefined'
                  && modalParent.length > 0) {
                  arguments[0].dropdownParent = modalParent;
                  }
                  return oldSelect2.apply(this,arguments);
                  };
                  })();





                  share|improve this answer































                    0














                    I just get it working by including select2.min.css



                    Try iy out



                    My modal html of bootstrap 3 is



                    <div id="changeTransportUserRequestPopup" class="modal fade" role="dialog">
                    <div class="modal-dialog" style="width: 40%!important; ">
                    <div class="modal-content">
                    <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h3>Warning</h3>
                    </div>
                    <div class="modal-body" id="changeTransportUserRequestPopupBody">
                    <select id="cbTransport" class="js-example-basic-single" name="cbTransport" style="width:100%!important;"></select>
                    </div>
                    <div class="modal-footer">
                    <button id="noPost" class="btn btn-default" name="postConfirm" value="false" data-dismiss="modal">Cancel</button>
                    <button type="submit" id="yesChangeTransportPost" class="btn btn-success" name="yesChangeTransportPost" value="true" data-dismiss="modal">Yes</button>
                    </div>
                    </div>
                    </div>
                    </div>





                    share|improve this answer





























                      0














                      I had a semi-related issue in an application so I'll put in my 2c.



                      I have multiple modals with forms containing select2 widgets. Opening modal A, then another modal inside modal A, would cause select2 widgets inside modal B to disappear and fail to initialize.



                      Each of these modals were loading the forms via ajax.



                      The solution was to remove the forms from the dom when closing a modal.



                      $(document).on('hidden.bs.modal', '.modal', function(e) {
                      // make sure we don't leave any select2 widgets around
                      $(this).find('.my-form').remove();
                      });





                      share|improve this answer




















                        protected by Community Jan 6 '17 at 12:09



                        Thank you for your interest in this question.
                        Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                        Would you like to answer one of these unanswered questions instead?














                        21 Answers
                        21






                        active

                        oldest

                        votes








                        21 Answers
                        21






                        active

                        oldest

                        votes









                        active

                        oldest

                        votes






                        active

                        oldest

                        votes









                        508














                        Ok, I've got it to work.



                        change



                        <div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h3 id="myModalLabel">Panel</h3>
                        </div>
                        <div class="modal-body" style="max-height: 800px">


                        to



                        <div id="myModal" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h3 id="myModalLabel">Panel</h3>
                        </div>
                        <div class="modal-body" style="max-height: 800px">


                        (remove tabindex="-1" from modal)






                        share|improve this answer



















                        • 18




                          May be there another solution? Because modal window then won't close by Esc key.
                          – woto
                          Oct 20 '13 at 15:47






                        • 18




                          removing tabindex="-1" didn't work form me. I've just changed <div class="modal-body"> to <div class="modal-body" style="overflow:hidden;">
                          – Wissem
                          Nov 27 '13 at 8:49






                        • 28




                          How the hell you discovered that ?
                          – DontVoteMeDown
                          Oct 1 '14 at 20:03






                        • 6




                          I have tried all options , but nothing works for me. I am using select2 4.0
                          – Girish Gupta
                          Sep 4 '15 at 4:19






                        • 4




                          Thanks! It's been 4 years and it's still a valid fix.
                          – Linek
                          Jul 21 '17 at 12:40
















                        508














                        Ok, I've got it to work.



                        change



                        <div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h3 id="myModalLabel">Panel</h3>
                        </div>
                        <div class="modal-body" style="max-height: 800px">


                        to



                        <div id="myModal" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h3 id="myModalLabel">Panel</h3>
                        </div>
                        <div class="modal-body" style="max-height: 800px">


                        (remove tabindex="-1" from modal)






                        share|improve this answer



















                        • 18




                          May be there another solution? Because modal window then won't close by Esc key.
                          – woto
                          Oct 20 '13 at 15:47






                        • 18




                          removing tabindex="-1" didn't work form me. I've just changed <div class="modal-body"> to <div class="modal-body" style="overflow:hidden;">
                          – Wissem
                          Nov 27 '13 at 8:49






                        • 28




                          How the hell you discovered that ?
                          – DontVoteMeDown
                          Oct 1 '14 at 20:03






                        • 6




                          I have tried all options , but nothing works for me. I am using select2 4.0
                          – Girish Gupta
                          Sep 4 '15 at 4:19






                        • 4




                          Thanks! It's been 4 years and it's still a valid fix.
                          – Linek
                          Jul 21 '17 at 12:40














                        508












                        508








                        508






                        Ok, I've got it to work.



                        change



                        <div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h3 id="myModalLabel">Panel</h3>
                        </div>
                        <div class="modal-body" style="max-height: 800px">


                        to



                        <div id="myModal" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h3 id="myModalLabel">Panel</h3>
                        </div>
                        <div class="modal-body" style="max-height: 800px">


                        (remove tabindex="-1" from modal)






                        share|improve this answer














                        Ok, I've got it to work.



                        change



                        <div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h3 id="myModalLabel">Panel</h3>
                        </div>
                        <div class="modal-body" style="max-height: 800px">


                        to



                        <div id="myModal" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h3 id="myModalLabel">Panel</h3>
                        </div>
                        <div class="modal-body" style="max-height: 800px">


                        (remove tabindex="-1" from modal)







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Jan 19 '17 at 11:02









                        Gowri

                        9,2202482143




                        9,2202482143










                        answered Aug 28 '13 at 12:07









                        breq

                        12.9k154481




                        12.9k154481








                        • 18




                          May be there another solution? Because modal window then won't close by Esc key.
                          – woto
                          Oct 20 '13 at 15:47






                        • 18




                          removing tabindex="-1" didn't work form me. I've just changed <div class="modal-body"> to <div class="modal-body" style="overflow:hidden;">
                          – Wissem
                          Nov 27 '13 at 8:49






                        • 28




                          How the hell you discovered that ?
                          – DontVoteMeDown
                          Oct 1 '14 at 20:03






                        • 6




                          I have tried all options , but nothing works for me. I am using select2 4.0
                          – Girish Gupta
                          Sep 4 '15 at 4:19






                        • 4




                          Thanks! It's been 4 years and it's still a valid fix.
                          – Linek
                          Jul 21 '17 at 12:40














                        • 18




                          May be there another solution? Because modal window then won't close by Esc key.
                          – woto
                          Oct 20 '13 at 15:47






                        • 18




                          removing tabindex="-1" didn't work form me. I've just changed <div class="modal-body"> to <div class="modal-body" style="overflow:hidden;">
                          – Wissem
                          Nov 27 '13 at 8:49






                        • 28




                          How the hell you discovered that ?
                          – DontVoteMeDown
                          Oct 1 '14 at 20:03






                        • 6




                          I have tried all options , but nothing works for me. I am using select2 4.0
                          – Girish Gupta
                          Sep 4 '15 at 4:19






                        • 4




                          Thanks! It's been 4 years and it's still a valid fix.
                          – Linek
                          Jul 21 '17 at 12:40








                        18




                        18




                        May be there another solution? Because modal window then won't close by Esc key.
                        – woto
                        Oct 20 '13 at 15:47




                        May be there another solution? Because modal window then won't close by Esc key.
                        – woto
                        Oct 20 '13 at 15:47




                        18




                        18




                        removing tabindex="-1" didn't work form me. I've just changed <div class="modal-body"> to <div class="modal-body" style="overflow:hidden;">
                        – Wissem
                        Nov 27 '13 at 8:49




                        removing tabindex="-1" didn't work form me. I've just changed <div class="modal-body"> to <div class="modal-body" style="overflow:hidden;">
                        – Wissem
                        Nov 27 '13 at 8:49




                        28




                        28




                        How the hell you discovered that ?
                        – DontVoteMeDown
                        Oct 1 '14 at 20:03




                        How the hell you discovered that ?
                        – DontVoteMeDown
                        Oct 1 '14 at 20:03




                        6




                        6




                        I have tried all options , but nothing works for me. I am using select2 4.0
                        – Girish Gupta
                        Sep 4 '15 at 4:19




                        I have tried all options , but nothing works for me. I am using select2 4.0
                        – Girish Gupta
                        Sep 4 '15 at 4:19




                        4




                        4




                        Thanks! It's been 4 years and it's still a valid fix.
                        – Linek
                        Jul 21 '17 at 12:40




                        Thanks! It's been 4 years and it's still a valid fix.
                        – Linek
                        Jul 21 '17 at 12:40













                        182














                        For Select2 v4:



                        Use dropdownParent to attach the dropdown to the modal dialog, rather than the HTML body.



                        <!-- Modal -->
                        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                        <div class="modal-dialog" role="document">
                        <div class="modal-content">
                        <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                        </div>
                        <div class="modal-body">
                        <select id="select2insidemodal" multiple="multiple">
                        <option value="AL">Alabama</option>
                        ...
                        <option value="WY">Wyoming</option>
                        </select>
                        </div>
                        <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button type="button" class="btn btn-primary">Save changes</button>
                        </div>
                        </div>
                        </div>
                        </div>


                        <script>

                        $(document).ready(function() {
                        $("#select2insidemodal").select2({
                        dropdownParent: $("#myModal")
                        });
                        });

                        </script>


                        This will attach the Select2 dropdown so it falls within the DOM of the modal rather than to the HTML body (the default). See https://select2.org/dropdown#dropdown-placement






                        share|improve this answer



















                        • 8




                          Confirmed, this is working like a charm and seems to be the best solution to this problem!
                          – lacco
                          Dec 7 '15 at 17:30






                        • 25




                          This worked for me (with v4.0.2), but I had to use dropdownParent: $("#myModal"), not dropdownParent: "#myModal".
                          – Saxon Druce
                          Apr 12 '16 at 7:58






                        • 4




                          @SaxonDruce This worked! Thanks for the extra info.
                          – Rohmer
                          May 7 '16 at 3:11






                        • 2




                          Thanks, this worked for me!
                          – Luis de Haro
                          Sep 2 '16 at 14:00






                        • 3




                          FWIW, in my case I needed to set the dropdownParent to the body of the modal, attaching it to the modal itself did not bring the z-index up high enough. e.g. dropdownParent: $('.modal-body', '#myModal')
                          – DrewInTheMountains
                          Dec 14 '17 at 18:03
















                        182














                        For Select2 v4:



                        Use dropdownParent to attach the dropdown to the modal dialog, rather than the HTML body.



                        <!-- Modal -->
                        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                        <div class="modal-dialog" role="document">
                        <div class="modal-content">
                        <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                        </div>
                        <div class="modal-body">
                        <select id="select2insidemodal" multiple="multiple">
                        <option value="AL">Alabama</option>
                        ...
                        <option value="WY">Wyoming</option>
                        </select>
                        </div>
                        <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button type="button" class="btn btn-primary">Save changes</button>
                        </div>
                        </div>
                        </div>
                        </div>


                        <script>

                        $(document).ready(function() {
                        $("#select2insidemodal").select2({
                        dropdownParent: $("#myModal")
                        });
                        });

                        </script>


                        This will attach the Select2 dropdown so it falls within the DOM of the modal rather than to the HTML body (the default). See https://select2.org/dropdown#dropdown-placement






                        share|improve this answer



















                        • 8




                          Confirmed, this is working like a charm and seems to be the best solution to this problem!
                          – lacco
                          Dec 7 '15 at 17:30






                        • 25




                          This worked for me (with v4.0.2), but I had to use dropdownParent: $("#myModal"), not dropdownParent: "#myModal".
                          – Saxon Druce
                          Apr 12 '16 at 7:58






                        • 4




                          @SaxonDruce This worked! Thanks for the extra info.
                          – Rohmer
                          May 7 '16 at 3:11






                        • 2




                          Thanks, this worked for me!
                          – Luis de Haro
                          Sep 2 '16 at 14:00






                        • 3




                          FWIW, in my case I needed to set the dropdownParent to the body of the modal, attaching it to the modal itself did not bring the z-index up high enough. e.g. dropdownParent: $('.modal-body', '#myModal')
                          – DrewInTheMountains
                          Dec 14 '17 at 18:03














                        182












                        182








                        182






                        For Select2 v4:



                        Use dropdownParent to attach the dropdown to the modal dialog, rather than the HTML body.



                        <!-- Modal -->
                        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                        <div class="modal-dialog" role="document">
                        <div class="modal-content">
                        <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                        </div>
                        <div class="modal-body">
                        <select id="select2insidemodal" multiple="multiple">
                        <option value="AL">Alabama</option>
                        ...
                        <option value="WY">Wyoming</option>
                        </select>
                        </div>
                        <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button type="button" class="btn btn-primary">Save changes</button>
                        </div>
                        </div>
                        </div>
                        </div>


                        <script>

                        $(document).ready(function() {
                        $("#select2insidemodal").select2({
                        dropdownParent: $("#myModal")
                        });
                        });

                        </script>


                        This will attach the Select2 dropdown so it falls within the DOM of the modal rather than to the HTML body (the default). See https://select2.org/dropdown#dropdown-placement






                        share|improve this answer














                        For Select2 v4:



                        Use dropdownParent to attach the dropdown to the modal dialog, rather than the HTML body.



                        <!-- Modal -->
                        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                        <div class="modal-dialog" role="document">
                        <div class="modal-content">
                        <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                        </div>
                        <div class="modal-body">
                        <select id="select2insidemodal" multiple="multiple">
                        <option value="AL">Alabama</option>
                        ...
                        <option value="WY">Wyoming</option>
                        </select>
                        </div>
                        <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button type="button" class="btn btn-primary">Save changes</button>
                        </div>
                        </div>
                        </div>
                        </div>


                        <script>

                        $(document).ready(function() {
                        $("#select2insidemodal").select2({
                        dropdownParent: $("#myModal")
                        });
                        });

                        </script>


                        This will attach the Select2 dropdown so it falls within the DOM of the modal rather than to the HTML body (the default). See https://select2.org/dropdown#dropdown-placement







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Sep 27 at 10:22









                        odlp

                        3,0091732




                        3,0091732










                        answered Nov 24 '15 at 1:53









                        dboswell

                        1,946196




                        1,946196








                        • 8




                          Confirmed, this is working like a charm and seems to be the best solution to this problem!
                          – lacco
                          Dec 7 '15 at 17:30






                        • 25




                          This worked for me (with v4.0.2), but I had to use dropdownParent: $("#myModal"), not dropdownParent: "#myModal".
                          – Saxon Druce
                          Apr 12 '16 at 7:58






                        • 4




                          @SaxonDruce This worked! Thanks for the extra info.
                          – Rohmer
                          May 7 '16 at 3:11






                        • 2




                          Thanks, this worked for me!
                          – Luis de Haro
                          Sep 2 '16 at 14:00






                        • 3




                          FWIW, in my case I needed to set the dropdownParent to the body of the modal, attaching it to the modal itself did not bring the z-index up high enough. e.g. dropdownParent: $('.modal-body', '#myModal')
                          – DrewInTheMountains
                          Dec 14 '17 at 18:03














                        • 8




                          Confirmed, this is working like a charm and seems to be the best solution to this problem!
                          – lacco
                          Dec 7 '15 at 17:30






                        • 25




                          This worked for me (with v4.0.2), but I had to use dropdownParent: $("#myModal"), not dropdownParent: "#myModal".
                          – Saxon Druce
                          Apr 12 '16 at 7:58






                        • 4




                          @SaxonDruce This worked! Thanks for the extra info.
                          – Rohmer
                          May 7 '16 at 3:11






                        • 2




                          Thanks, this worked for me!
                          – Luis de Haro
                          Sep 2 '16 at 14:00






                        • 3




                          FWIW, in my case I needed to set the dropdownParent to the body of the modal, attaching it to the modal itself did not bring the z-index up high enough. e.g. dropdownParent: $('.modal-body', '#myModal')
                          – DrewInTheMountains
                          Dec 14 '17 at 18:03








                        8




                        8




                        Confirmed, this is working like a charm and seems to be the best solution to this problem!
                        – lacco
                        Dec 7 '15 at 17:30




                        Confirmed, this is working like a charm and seems to be the best solution to this problem!
                        – lacco
                        Dec 7 '15 at 17:30




                        25




                        25




                        This worked for me (with v4.0.2), but I had to use dropdownParent: $("#myModal"), not dropdownParent: "#myModal".
                        – Saxon Druce
                        Apr 12 '16 at 7:58




                        This worked for me (with v4.0.2), but I had to use dropdownParent: $("#myModal"), not dropdownParent: "#myModal".
                        – Saxon Druce
                        Apr 12 '16 at 7:58




                        4




                        4




                        @SaxonDruce This worked! Thanks for the extra info.
                        – Rohmer
                        May 7 '16 at 3:11




                        @SaxonDruce This worked! Thanks for the extra info.
                        – Rohmer
                        May 7 '16 at 3:11




                        2




                        2




                        Thanks, this worked for me!
                        – Luis de Haro
                        Sep 2 '16 at 14:00




                        Thanks, this worked for me!
                        – Luis de Haro
                        Sep 2 '16 at 14:00




                        3




                        3




                        FWIW, in my case I needed to set the dropdownParent to the body of the modal, attaching it to the modal itself did not bring the z-index up high enough. e.g. dropdownParent: $('.modal-body', '#myModal')
                        – DrewInTheMountains
                        Dec 14 '17 at 18:03




                        FWIW, in my case I needed to set the dropdownParent to the body of the modal, attaching it to the modal itself did not bring the z-index up high enough. e.g. dropdownParent: $('.modal-body', '#myModal')
                        – DrewInTheMountains
                        Dec 14 '17 at 18:03











                        159














                        I found a solution to this on github for select2



                        https://github.com/ivaynberg/select2/issues/1436



                        For bootstrap 3, the solution is:



                        $.fn.modal.Constructor.prototype.enforceFocus = function() {};


                        Bootstrap 4 renamed the enforceFocus method to _enforceFocus, so you'll need to patch that instead:



                        $.fn.modal.Constructor.prototype._enforceFocus = function() {};


                        Explanation copied from link above:




                        Bootstrap registers a listener to the focusin event which checks whether the focused element is either the overlay itself or a descendent of it - if not it just refocuses on the overlay. With the select2 dropdown being attached to the body this effectively prevents you from entering anything into the the textfield.



                        You can quickfix this by overwriting the enforceFocus function which registers the event on the modal







                        share|improve this answer



















                        • 1




                          @pymarco That one should have been there a little earlier to be picked as the answer! Thanks man, for providing a real answer to a really annoying problem.
                          – ilter
                          Dec 30 '13 at 23:44








                        • 2




                          @pymarco This is the correct solution. Thank you for saving my time.
                          – VKPRO
                          Jan 12 '15 at 8:57






                        • 2




                          You saved me loads of time with an issue that didn't always rear it's head. Thank you!
                          – user3036342
                          May 18 '15 at 13:47






                        • 1




                          This worked for me!! I was browsing and browsing through the issues threads for select2 in github and didn't run into that post you provided. It was googling and finding this page in stackoverflow what saved my life. Thank you!
                          – luis.ap.uyen
                          Jan 19 '16 at 9:27






                        • 2




                          where do I put this code and what function should it address? Sorry, JS noob here, I tried making this a part of the select2 JS or placing it at top / end of the page, all without success. $.fn.modal.Constructor.prototype.enforceFocus = $.noop;
                          – Armitage2k
                          Nov 27 '16 at 0:59
















                        159














                        I found a solution to this on github for select2



                        https://github.com/ivaynberg/select2/issues/1436



                        For bootstrap 3, the solution is:



                        $.fn.modal.Constructor.prototype.enforceFocus = function() {};


                        Bootstrap 4 renamed the enforceFocus method to _enforceFocus, so you'll need to patch that instead:



                        $.fn.modal.Constructor.prototype._enforceFocus = function() {};


                        Explanation copied from link above:




                        Bootstrap registers a listener to the focusin event which checks whether the focused element is either the overlay itself or a descendent of it - if not it just refocuses on the overlay. With the select2 dropdown being attached to the body this effectively prevents you from entering anything into the the textfield.



                        You can quickfix this by overwriting the enforceFocus function which registers the event on the modal







                        share|improve this answer



















                        • 1




                          @pymarco That one should have been there a little earlier to be picked as the answer! Thanks man, for providing a real answer to a really annoying problem.
                          – ilter
                          Dec 30 '13 at 23:44








                        • 2




                          @pymarco This is the correct solution. Thank you for saving my time.
                          – VKPRO
                          Jan 12 '15 at 8:57






                        • 2




                          You saved me loads of time with an issue that didn't always rear it's head. Thank you!
                          – user3036342
                          May 18 '15 at 13:47






                        • 1




                          This worked for me!! I was browsing and browsing through the issues threads for select2 in github and didn't run into that post you provided. It was googling and finding this page in stackoverflow what saved my life. Thank you!
                          – luis.ap.uyen
                          Jan 19 '16 at 9:27






                        • 2




                          where do I put this code and what function should it address? Sorry, JS noob here, I tried making this a part of the select2 JS or placing it at top / end of the page, all without success. $.fn.modal.Constructor.prototype.enforceFocus = $.noop;
                          – Armitage2k
                          Nov 27 '16 at 0:59














                        159












                        159








                        159






                        I found a solution to this on github for select2



                        https://github.com/ivaynberg/select2/issues/1436



                        For bootstrap 3, the solution is:



                        $.fn.modal.Constructor.prototype.enforceFocus = function() {};


                        Bootstrap 4 renamed the enforceFocus method to _enforceFocus, so you'll need to patch that instead:



                        $.fn.modal.Constructor.prototype._enforceFocus = function() {};


                        Explanation copied from link above:




                        Bootstrap registers a listener to the focusin event which checks whether the focused element is either the overlay itself or a descendent of it - if not it just refocuses on the overlay. With the select2 dropdown being attached to the body this effectively prevents you from entering anything into the the textfield.



                        You can quickfix this by overwriting the enforceFocus function which registers the event on the modal







                        share|improve this answer














                        I found a solution to this on github for select2



                        https://github.com/ivaynberg/select2/issues/1436



                        For bootstrap 3, the solution is:



                        $.fn.modal.Constructor.prototype.enforceFocus = function() {};


                        Bootstrap 4 renamed the enforceFocus method to _enforceFocus, so you'll need to patch that instead:



                        $.fn.modal.Constructor.prototype._enforceFocus = function() {};


                        Explanation copied from link above:




                        Bootstrap registers a listener to the focusin event which checks whether the focused element is either the overlay itself or a descendent of it - if not it just refocuses on the overlay. With the select2 dropdown being attached to the body this effectively prevents you from entering anything into the the textfield.



                        You can quickfix this by overwriting the enforceFocus function which registers the event on the modal








                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Oct 9 at 16:41









                        jaapz

                        597520




                        597520










                        answered Oct 24 '13 at 18:46









                        pymarco

                        3,74941932




                        3,74941932








                        • 1




                          @pymarco That one should have been there a little earlier to be picked as the answer! Thanks man, for providing a real answer to a really annoying problem.
                          – ilter
                          Dec 30 '13 at 23:44








                        • 2




                          @pymarco This is the correct solution. Thank you for saving my time.
                          – VKPRO
                          Jan 12 '15 at 8:57






                        • 2




                          You saved me loads of time with an issue that didn't always rear it's head. Thank you!
                          – user3036342
                          May 18 '15 at 13:47






                        • 1




                          This worked for me!! I was browsing and browsing through the issues threads for select2 in github and didn't run into that post you provided. It was googling and finding this page in stackoverflow what saved my life. Thank you!
                          – luis.ap.uyen
                          Jan 19 '16 at 9:27






                        • 2




                          where do I put this code and what function should it address? Sorry, JS noob here, I tried making this a part of the select2 JS or placing it at top / end of the page, all without success. $.fn.modal.Constructor.prototype.enforceFocus = $.noop;
                          – Armitage2k
                          Nov 27 '16 at 0:59














                        • 1




                          @pymarco That one should have been there a little earlier to be picked as the answer! Thanks man, for providing a real answer to a really annoying problem.
                          – ilter
                          Dec 30 '13 at 23:44








                        • 2




                          @pymarco This is the correct solution. Thank you for saving my time.
                          – VKPRO
                          Jan 12 '15 at 8:57






                        • 2




                          You saved me loads of time with an issue that didn't always rear it's head. Thank you!
                          – user3036342
                          May 18 '15 at 13:47






                        • 1




                          This worked for me!! I was browsing and browsing through the issues threads for select2 in github and didn't run into that post you provided. It was googling and finding this page in stackoverflow what saved my life. Thank you!
                          – luis.ap.uyen
                          Jan 19 '16 at 9:27






                        • 2




                          where do I put this code and what function should it address? Sorry, JS noob here, I tried making this a part of the select2 JS or placing it at top / end of the page, all without success. $.fn.modal.Constructor.prototype.enforceFocus = $.noop;
                          – Armitage2k
                          Nov 27 '16 at 0:59








                        1




                        1




                        @pymarco That one should have been there a little earlier to be picked as the answer! Thanks man, for providing a real answer to a really annoying problem.
                        – ilter
                        Dec 30 '13 at 23:44






                        @pymarco That one should have been there a little earlier to be picked as the answer! Thanks man, for providing a real answer to a really annoying problem.
                        – ilter
                        Dec 30 '13 at 23:44






                        2




                        2




                        @pymarco This is the correct solution. Thank you for saving my time.
                        – VKPRO
                        Jan 12 '15 at 8:57




                        @pymarco This is the correct solution. Thank you for saving my time.
                        – VKPRO
                        Jan 12 '15 at 8:57




                        2




                        2




                        You saved me loads of time with an issue that didn't always rear it's head. Thank you!
                        – user3036342
                        May 18 '15 at 13:47




                        You saved me loads of time with an issue that didn't always rear it's head. Thank you!
                        – user3036342
                        May 18 '15 at 13:47




                        1




                        1




                        This worked for me!! I was browsing and browsing through the issues threads for select2 in github and didn't run into that post you provided. It was googling and finding this page in stackoverflow what saved my life. Thank you!
                        – luis.ap.uyen
                        Jan 19 '16 at 9:27




                        This worked for me!! I was browsing and browsing through the issues threads for select2 in github and didn't run into that post you provided. It was googling and finding this page in stackoverflow what saved my life. Thank you!
                        – luis.ap.uyen
                        Jan 19 '16 at 9:27




                        2




                        2




                        where do I put this code and what function should it address? Sorry, JS noob here, I tried making this a part of the select2 JS or placing it at top / end of the page, all without success. $.fn.modal.Constructor.prototype.enforceFocus = $.noop;
                        – Armitage2k
                        Nov 27 '16 at 0:59




                        where do I put this code and what function should it address? Sorry, JS noob here, I tried making this a part of the select2 JS or placing it at top / end of the page, all without success. $.fn.modal.Constructor.prototype.enforceFocus = $.noop;
                        – Armitage2k
                        Nov 27 '16 at 0:59











                        29














                        Just remove tabindex="-1" and add style overflow:hidden



                        Here is an example:



                        <div id="myModal" class="modal fade" role="dialog" style="overflow:hidden;">
                        <!---content modal here -->
                        </div>





                        share|improve this answer























                        • This one worked for me! (Firefox & Chrome)
                          – Julian Koster
                          Jul 31 '17 at 14:32












                        • it works but when I have long modal I can't scroll down !
                          – Tarek Kalaji
                          Mar 17 at 16:40






                        • 1




                          @TarekKalaji did you ever figure out how to allow scrolling with this solution?
                          – Cameron Taylor
                          Mar 28 at 13:20










                        • Tried removing the tabindex but was not enought. Adding overflow: hidden is useful if the modal box is long...
                          – Plasebo
                          Apr 2 at 11:17










                        • Worked for me removing the tab index attribute.
                          – Glen
                          Jun 4 at 11:25
















                        29














                        Just remove tabindex="-1" and add style overflow:hidden



                        Here is an example:



                        <div id="myModal" class="modal fade" role="dialog" style="overflow:hidden;">
                        <!---content modal here -->
                        </div>





                        share|improve this answer























                        • This one worked for me! (Firefox & Chrome)
                          – Julian Koster
                          Jul 31 '17 at 14:32












                        • it works but when I have long modal I can't scroll down !
                          – Tarek Kalaji
                          Mar 17 at 16:40






                        • 1




                          @TarekKalaji did you ever figure out how to allow scrolling with this solution?
                          – Cameron Taylor
                          Mar 28 at 13:20










                        • Tried removing the tabindex but was not enought. Adding overflow: hidden is useful if the modal box is long...
                          – Plasebo
                          Apr 2 at 11:17










                        • Worked for me removing the tab index attribute.
                          – Glen
                          Jun 4 at 11:25














                        29












                        29








                        29






                        Just remove tabindex="-1" and add style overflow:hidden



                        Here is an example:



                        <div id="myModal" class="modal fade" role="dialog" style="overflow:hidden;">
                        <!---content modal here -->
                        </div>





                        share|improve this answer














                        Just remove tabindex="-1" and add style overflow:hidden



                        Here is an example:



                        <div id="myModal" class="modal fade" role="dialog" style="overflow:hidden;">
                        <!---content modal here -->
                        </div>






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Jan 9 at 0:16









                        dferenc

                        4,513122031




                        4,513122031










                        answered Dec 16 '16 at 15:09









                        MrBii

                        1,144129




                        1,144129












                        • This one worked for me! (Firefox & Chrome)
                          – Julian Koster
                          Jul 31 '17 at 14:32












                        • it works but when I have long modal I can't scroll down !
                          – Tarek Kalaji
                          Mar 17 at 16:40






                        • 1




                          @TarekKalaji did you ever figure out how to allow scrolling with this solution?
                          – Cameron Taylor
                          Mar 28 at 13:20










                        • Tried removing the tabindex but was not enought. Adding overflow: hidden is useful if the modal box is long...
                          – Plasebo
                          Apr 2 at 11:17










                        • Worked for me removing the tab index attribute.
                          – Glen
                          Jun 4 at 11:25


















                        • This one worked for me! (Firefox & Chrome)
                          – Julian Koster
                          Jul 31 '17 at 14:32












                        • it works but when I have long modal I can't scroll down !
                          – Tarek Kalaji
                          Mar 17 at 16:40






                        • 1




                          @TarekKalaji did you ever figure out how to allow scrolling with this solution?
                          – Cameron Taylor
                          Mar 28 at 13:20










                        • Tried removing the tabindex but was not enought. Adding overflow: hidden is useful if the modal box is long...
                          – Plasebo
                          Apr 2 at 11:17










                        • Worked for me removing the tab index attribute.
                          – Glen
                          Jun 4 at 11:25
















                        This one worked for me! (Firefox & Chrome)
                        – Julian Koster
                        Jul 31 '17 at 14:32






                        This one worked for me! (Firefox & Chrome)
                        – Julian Koster
                        Jul 31 '17 at 14:32














                        it works but when I have long modal I can't scroll down !
                        – Tarek Kalaji
                        Mar 17 at 16:40




                        it works but when I have long modal I can't scroll down !
                        – Tarek Kalaji
                        Mar 17 at 16:40




                        1




                        1




                        @TarekKalaji did you ever figure out how to allow scrolling with this solution?
                        – Cameron Taylor
                        Mar 28 at 13:20




                        @TarekKalaji did you ever figure out how to allow scrolling with this solution?
                        – Cameron Taylor
                        Mar 28 at 13:20












                        Tried removing the tabindex but was not enought. Adding overflow: hidden is useful if the modal box is long...
                        – Plasebo
                        Apr 2 at 11:17




                        Tried removing the tabindex but was not enought. Adding overflow: hidden is useful if the modal box is long...
                        – Plasebo
                        Apr 2 at 11:17












                        Worked for me removing the tab index attribute.
                        – Glen
                        Jun 4 at 11:25




                        Worked for me removing the tab index attribute.
                        – Glen
                        Jun 4 at 11:25











                        23














                        .select2-close-mask{
                        z-index: 2099;
                        }
                        .select2-dropdown{
                        z-index: 3051;
                        }


                        This is my solution with select2 4.0.0. Just override the css right below the select2.css import.
                        Please make sure the z-index is greater than your dialog or modal. I just add 2000 on the default ones. Cause my dialogs' z-index are about 1000.






                        share|improve this answer

















                        • 1




                          None of the solutions worked for select2 4.0.0 except this. Thanks a lot.
                          – Neel
                          Jun 26 '15 at 10:15










                        • The same issue occurs if Select2 (v4.0.0) is in a popover. In bootstrap.css the z-index of .popover is set to 1060 so this fix works as well. I like it because it's simple and it solves the problem the same way the problem was created: z-index set in a style sheet.
                          – Paul
                          Jul 2 '15 at 18:02








                        • 1




                          The most trivial yet the only solution that worked. Thanks!
                          – Ishtiaque Khan
                          Oct 17 '15 at 1:23










                        • I've added the above in my css file, but it has no changes. I'm using jQuery Mobile. Any suggestions please?
                          – Sahil Khanna
                          Feb 20 '16 at 7:03










                        • @Sahil 1. make sure it's successfully overridden the original ones. simple way is add this after all the default css files2.check the z-index, ensure it's larger than the dialog's
                          – Diluka W
                          Feb 22 '16 at 8:55
















                        23














                        .select2-close-mask{
                        z-index: 2099;
                        }
                        .select2-dropdown{
                        z-index: 3051;
                        }


                        This is my solution with select2 4.0.0. Just override the css right below the select2.css import.
                        Please make sure the z-index is greater than your dialog or modal. I just add 2000 on the default ones. Cause my dialogs' z-index are about 1000.






                        share|improve this answer

















                        • 1




                          None of the solutions worked for select2 4.0.0 except this. Thanks a lot.
                          – Neel
                          Jun 26 '15 at 10:15










                        • The same issue occurs if Select2 (v4.0.0) is in a popover. In bootstrap.css the z-index of .popover is set to 1060 so this fix works as well. I like it because it's simple and it solves the problem the same way the problem was created: z-index set in a style sheet.
                          – Paul
                          Jul 2 '15 at 18:02








                        • 1




                          The most trivial yet the only solution that worked. Thanks!
                          – Ishtiaque Khan
                          Oct 17 '15 at 1:23










                        • I've added the above in my css file, but it has no changes. I'm using jQuery Mobile. Any suggestions please?
                          – Sahil Khanna
                          Feb 20 '16 at 7:03










                        • @Sahil 1. make sure it's successfully overridden the original ones. simple way is add this after all the default css files2.check the z-index, ensure it's larger than the dialog's
                          – Diluka W
                          Feb 22 '16 at 8:55














                        23












                        23








                        23






                        .select2-close-mask{
                        z-index: 2099;
                        }
                        .select2-dropdown{
                        z-index: 3051;
                        }


                        This is my solution with select2 4.0.0. Just override the css right below the select2.css import.
                        Please make sure the z-index is greater than your dialog or modal. I just add 2000 on the default ones. Cause my dialogs' z-index are about 1000.






                        share|improve this answer












                        .select2-close-mask{
                        z-index: 2099;
                        }
                        .select2-dropdown{
                        z-index: 3051;
                        }


                        This is my solution with select2 4.0.0. Just override the css right below the select2.css import.
                        Please make sure the z-index is greater than your dialog or modal. I just add 2000 on the default ones. Cause my dialogs' z-index are about 1000.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered May 6 '15 at 8:25









                        Diluka W

                        31628




                        31628








                        • 1




                          None of the solutions worked for select2 4.0.0 except this. Thanks a lot.
                          – Neel
                          Jun 26 '15 at 10:15










                        • The same issue occurs if Select2 (v4.0.0) is in a popover. In bootstrap.css the z-index of .popover is set to 1060 so this fix works as well. I like it because it's simple and it solves the problem the same way the problem was created: z-index set in a style sheet.
                          – Paul
                          Jul 2 '15 at 18:02








                        • 1




                          The most trivial yet the only solution that worked. Thanks!
                          – Ishtiaque Khan
                          Oct 17 '15 at 1:23










                        • I've added the above in my css file, but it has no changes. I'm using jQuery Mobile. Any suggestions please?
                          – Sahil Khanna
                          Feb 20 '16 at 7:03










                        • @Sahil 1. make sure it's successfully overridden the original ones. simple way is add this after all the default css files2.check the z-index, ensure it's larger than the dialog's
                          – Diluka W
                          Feb 22 '16 at 8:55














                        • 1




                          None of the solutions worked for select2 4.0.0 except this. Thanks a lot.
                          – Neel
                          Jun 26 '15 at 10:15










                        • The same issue occurs if Select2 (v4.0.0) is in a popover. In bootstrap.css the z-index of .popover is set to 1060 so this fix works as well. I like it because it's simple and it solves the problem the same way the problem was created: z-index set in a style sheet.
                          – Paul
                          Jul 2 '15 at 18:02








                        • 1




                          The most trivial yet the only solution that worked. Thanks!
                          – Ishtiaque Khan
                          Oct 17 '15 at 1:23










                        • I've added the above in my css file, but it has no changes. I'm using jQuery Mobile. Any suggestions please?
                          – Sahil Khanna
                          Feb 20 '16 at 7:03










                        • @Sahil 1. make sure it's successfully overridden the original ones. simple way is add this after all the default css files2.check the z-index, ensure it's larger than the dialog's
                          – Diluka W
                          Feb 22 '16 at 8:55








                        1




                        1




                        None of the solutions worked for select2 4.0.0 except this. Thanks a lot.
                        – Neel
                        Jun 26 '15 at 10:15




                        None of the solutions worked for select2 4.0.0 except this. Thanks a lot.
                        – Neel
                        Jun 26 '15 at 10:15












                        The same issue occurs if Select2 (v4.0.0) is in a popover. In bootstrap.css the z-index of .popover is set to 1060 so this fix works as well. I like it because it's simple and it solves the problem the same way the problem was created: z-index set in a style sheet.
                        – Paul
                        Jul 2 '15 at 18:02






                        The same issue occurs if Select2 (v4.0.0) is in a popover. In bootstrap.css the z-index of .popover is set to 1060 so this fix works as well. I like it because it's simple and it solves the problem the same way the problem was created: z-index set in a style sheet.
                        – Paul
                        Jul 2 '15 at 18:02






                        1




                        1




                        The most trivial yet the only solution that worked. Thanks!
                        – Ishtiaque Khan
                        Oct 17 '15 at 1:23




                        The most trivial yet the only solution that worked. Thanks!
                        – Ishtiaque Khan
                        Oct 17 '15 at 1:23












                        I've added the above in my css file, but it has no changes. I'm using jQuery Mobile. Any suggestions please?
                        – Sahil Khanna
                        Feb 20 '16 at 7:03




                        I've added the above in my css file, but it has no changes. I'm using jQuery Mobile. Any suggestions please?
                        – Sahil Khanna
                        Feb 20 '16 at 7:03












                        @Sahil 1. make sure it's successfully overridden the original ones. simple way is add this after all the default css files2.check the z-index, ensure it's larger than the dialog's
                        – Diluka W
                        Feb 22 '16 at 8:55




                        @Sahil 1. make sure it's successfully overridden the original ones. simple way is add this after all the default css files2.check the z-index, ensure it's larger than the dialog's
                        – Diluka W
                        Feb 22 '16 at 8:55











                        6














                        change select2.css file



                        z-index: 9998;
                        ...
                        z-index: 9999;
                        ...
                        z-index: 10000;


                        to



                        z-index: 10000;
                        ...
                        z-index: 10001;
                        ...
                        z-index: 10002;





                        share|improve this answer





















                        • That's the only one works for me (jquery-ui-1.10.3.css, jquery-ui-1.9.1.js and select2 3.4.0). I have 2 jquery UI dialogs one on top of the other and select2 on the second one. Also I have put //allow interaction of select2, date and time picker on jQuery UI dialog $.ui.dialog.prototype._allowInteraction = function(e) { return !!$(e.target).closest('.ui-dialog, .ui-datepicker, .select2-drop').length; }; I have no idea if this help or not.
                          – Adrian P.
                          Feb 9 '16 at 17:29


















                        6














                        change select2.css file



                        z-index: 9998;
                        ...
                        z-index: 9999;
                        ...
                        z-index: 10000;


                        to



                        z-index: 10000;
                        ...
                        z-index: 10001;
                        ...
                        z-index: 10002;





                        share|improve this answer





















                        • That's the only one works for me (jquery-ui-1.10.3.css, jquery-ui-1.9.1.js and select2 3.4.0). I have 2 jquery UI dialogs one on top of the other and select2 on the second one. Also I have put //allow interaction of select2, date and time picker on jQuery UI dialog $.ui.dialog.prototype._allowInteraction = function(e) { return !!$(e.target).closest('.ui-dialog, .ui-datepicker, .select2-drop').length; }; I have no idea if this help or not.
                          – Adrian P.
                          Feb 9 '16 at 17:29
















                        6












                        6








                        6






                        change select2.css file



                        z-index: 9998;
                        ...
                        z-index: 9999;
                        ...
                        z-index: 10000;


                        to



                        z-index: 10000;
                        ...
                        z-index: 10001;
                        ...
                        z-index: 10002;





                        share|improve this answer












                        change select2.css file



                        z-index: 9998;
                        ...
                        z-index: 9999;
                        ...
                        z-index: 10000;


                        to



                        z-index: 10000;
                        ...
                        z-index: 10001;
                        ...
                        z-index: 10002;






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Feb 12 '14 at 8:48









                        user1616435

                        6911




                        6911












                        • That's the only one works for me (jquery-ui-1.10.3.css, jquery-ui-1.9.1.js and select2 3.4.0). I have 2 jquery UI dialogs one on top of the other and select2 on the second one. Also I have put //allow interaction of select2, date and time picker on jQuery UI dialog $.ui.dialog.prototype._allowInteraction = function(e) { return !!$(e.target).closest('.ui-dialog, .ui-datepicker, .select2-drop').length; }; I have no idea if this help or not.
                          – Adrian P.
                          Feb 9 '16 at 17:29




















                        • That's the only one works for me (jquery-ui-1.10.3.css, jquery-ui-1.9.1.js and select2 3.4.0). I have 2 jquery UI dialogs one on top of the other and select2 on the second one. Also I have put //allow interaction of select2, date and time picker on jQuery UI dialog $.ui.dialog.prototype._allowInteraction = function(e) { return !!$(e.target).closest('.ui-dialog, .ui-datepicker, .select2-drop').length; }; I have no idea if this help or not.
                          – Adrian P.
                          Feb 9 '16 at 17:29


















                        That's the only one works for me (jquery-ui-1.10.3.css, jquery-ui-1.9.1.js and select2 3.4.0). I have 2 jquery UI dialogs one on top of the other and select2 on the second one. Also I have put //allow interaction of select2, date and time picker on jQuery UI dialog $.ui.dialog.prototype._allowInteraction = function(e) { return !!$(e.target).closest('.ui-dialog, .ui-datepicker, .select2-drop').length; }; I have no idea if this help or not.
                        – Adrian P.
                        Feb 9 '16 at 17:29






                        That's the only one works for me (jquery-ui-1.10.3.css, jquery-ui-1.9.1.js and select2 3.4.0). I have 2 jquery UI dialogs one on top of the other and select2 on the second one. Also I have put //allow interaction of select2, date and time picker on jQuery UI dialog $.ui.dialog.prototype._allowInteraction = function(e) { return !!$(e.target).closest('.ui-dialog, .ui-datepicker, .select2-drop').length; }; I have no idea if this help or not.
                        – Adrian P.
                        Feb 9 '16 at 17:29













                        6














                        I had the same issue, updating z-index for .select2-container should do the trick. Make sure your modal's z-index is lower than select2's.



                        .select2-container {
                        z-index: 99999;
                        }


                        Updated:
                        In case above code doesn't work properly, also remove tabindexes from your modal as @breq suggested






                        share|improve this answer



















                        • 1




                          you need to implement this with @breq answer to make it works.
                          – Ryan Arief
                          Jun 11 '17 at 3:40










                        • @RyanArief my bad, thanks.
                          – hhk
                          Jun 12 '17 at 7:44


















                        6














                        I had the same issue, updating z-index for .select2-container should do the trick. Make sure your modal's z-index is lower than select2's.



                        .select2-container {
                        z-index: 99999;
                        }


                        Updated:
                        In case above code doesn't work properly, also remove tabindexes from your modal as @breq suggested






                        share|improve this answer



















                        • 1




                          you need to implement this with @breq answer to make it works.
                          – Ryan Arief
                          Jun 11 '17 at 3:40










                        • @RyanArief my bad, thanks.
                          – hhk
                          Jun 12 '17 at 7:44
















                        6












                        6








                        6






                        I had the same issue, updating z-index for .select2-container should do the trick. Make sure your modal's z-index is lower than select2's.



                        .select2-container {
                        z-index: 99999;
                        }


                        Updated:
                        In case above code doesn't work properly, also remove tabindexes from your modal as @breq suggested






                        share|improve this answer














                        I had the same issue, updating z-index for .select2-container should do the trick. Make sure your modal's z-index is lower than select2's.



                        .select2-container {
                        z-index: 99999;
                        }


                        Updated:
                        In case above code doesn't work properly, also remove tabindexes from your modal as @breq suggested







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Jun 12 '17 at 7:47

























                        answered May 18 '17 at 8:54









                        hhk

                        250412




                        250412








                        • 1




                          you need to implement this with @breq answer to make it works.
                          – Ryan Arief
                          Jun 11 '17 at 3:40










                        • @RyanArief my bad, thanks.
                          – hhk
                          Jun 12 '17 at 7:44
















                        • 1




                          you need to implement this with @breq answer to make it works.
                          – Ryan Arief
                          Jun 11 '17 at 3:40










                        • @RyanArief my bad, thanks.
                          – hhk
                          Jun 12 '17 at 7:44










                        1




                        1




                        you need to implement this with @breq answer to make it works.
                        – Ryan Arief
                        Jun 11 '17 at 3:40




                        you need to implement this with @breq answer to make it works.
                        – Ryan Arief
                        Jun 11 '17 at 3:40












                        @RyanArief my bad, thanks.
                        – hhk
                        Jun 12 '17 at 7:44






                        @RyanArief my bad, thanks.
                        – hhk
                        Jun 12 '17 at 7:44













                        6














                        Jus use this code on Document.read()



                        $.fn.modal.Constructor.prototype.enforceFocus = function () {};





                        share|improve this answer

















                        • 1




                          The proper function name is "$(document).ready". So code should be like: $(document).ready(function () { $.fn.modal.Constructor.prototype.enforceFocus = function () { }; });
                          – Lech Osiński
                          Nov 2 '17 at 14:04








                        • 1




                          @LechOsiński - Thank you for actually describing how to use this little chunk of code. Your solution is the only one that worked for me. +1
                          – Francis Bartkowiak
                          Oct 18 at 0:50
















                        6














                        Jus use this code on Document.read()



                        $.fn.modal.Constructor.prototype.enforceFocus = function () {};





                        share|improve this answer

















                        • 1




                          The proper function name is "$(document).ready". So code should be like: $(document).ready(function () { $.fn.modal.Constructor.prototype.enforceFocus = function () { }; });
                          – Lech Osiński
                          Nov 2 '17 at 14:04








                        • 1




                          @LechOsiński - Thank you for actually describing how to use this little chunk of code. Your solution is the only one that worked for me. +1
                          – Francis Bartkowiak
                          Oct 18 at 0:50














                        6












                        6








                        6






                        Jus use this code on Document.read()



                        $.fn.modal.Constructor.prototype.enforceFocus = function () {};





                        share|improve this answer












                        Jus use this code on Document.read()



                        $.fn.modal.Constructor.prototype.enforceFocus = function () {};






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Jun 17 '17 at 5:42









                        S.Mohamed Mahdi Ahmadian zadeh

                        1,46311115




                        1,46311115








                        • 1




                          The proper function name is "$(document).ready". So code should be like: $(document).ready(function () { $.fn.modal.Constructor.prototype.enforceFocus = function () { }; });
                          – Lech Osiński
                          Nov 2 '17 at 14:04








                        • 1




                          @LechOsiński - Thank you for actually describing how to use this little chunk of code. Your solution is the only one that worked for me. +1
                          – Francis Bartkowiak
                          Oct 18 at 0:50














                        • 1




                          The proper function name is "$(document).ready". So code should be like: $(document).ready(function () { $.fn.modal.Constructor.prototype.enforceFocus = function () { }; });
                          – Lech Osiński
                          Nov 2 '17 at 14:04








                        • 1




                          @LechOsiński - Thank you for actually describing how to use this little chunk of code. Your solution is the only one that worked for me. +1
                          – Francis Bartkowiak
                          Oct 18 at 0:50








                        1




                        1




                        The proper function name is "$(document).ready". So code should be like: $(document).ready(function () { $.fn.modal.Constructor.prototype.enforceFocus = function () { }; });
                        – Lech Osiński
                        Nov 2 '17 at 14:04






                        The proper function name is "$(document).ready". So code should be like: $(document).ready(function () { $.fn.modal.Constructor.prototype.enforceFocus = function () { }; });
                        – Lech Osiński
                        Nov 2 '17 at 14:04






                        1




                        1




                        @LechOsiński - Thank you for actually describing how to use this little chunk of code. Your solution is the only one that worked for me. +1
                        – Francis Bartkowiak
                        Oct 18 at 0:50




                        @LechOsiński - Thank you for actually describing how to use this little chunk of code. Your solution is the only one that worked for me. +1
                        – Francis Bartkowiak
                        Oct 18 at 0:50











                        6














                        Answer that worked for me found here: https://github.com/select2/select2-bootstrap-theme/issues/41



                        $('select').select2({
                        dropdownParent: $('#my_amazing_modal')
                        });


                        Also doesn't require removing the tabindex.






                        share|improve this answer





















                        • This is the nicest way, also keeps all functionality from modal.
                          – Edofre
                          Feb 21 at 14:05
















                        6














                        Answer that worked for me found here: https://github.com/select2/select2-bootstrap-theme/issues/41



                        $('select').select2({
                        dropdownParent: $('#my_amazing_modal')
                        });


                        Also doesn't require removing the tabindex.






                        share|improve this answer





















                        • This is the nicest way, also keeps all functionality from modal.
                          – Edofre
                          Feb 21 at 14:05














                        6












                        6








                        6






                        Answer that worked for me found here: https://github.com/select2/select2-bootstrap-theme/issues/41



                        $('select').select2({
                        dropdownParent: $('#my_amazing_modal')
                        });


                        Also doesn't require removing the tabindex.






                        share|improve this answer












                        Answer that worked for me found here: https://github.com/select2/select2-bootstrap-theme/issues/41



                        $('select').select2({
                        dropdownParent: $('#my_amazing_modal')
                        });


                        Also doesn't require removing the tabindex.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 29 '17 at 14:53









                        Ashraf Slamang

                        31149




                        31149












                        • This is the nicest way, also keeps all functionality from modal.
                          – Edofre
                          Feb 21 at 14:05


















                        • This is the nicest way, also keeps all functionality from modal.
                          – Edofre
                          Feb 21 at 14:05
















                        This is the nicest way, also keeps all functionality from modal.
                        – Edofre
                        Feb 21 at 14:05




                        This is the nicest way, also keeps all functionality from modal.
                        – Edofre
                        Feb 21 at 14:05











                        5














                        Just to understand better how tabindex elements works to complete accepted answer :




                        The tabindex global attribute is an integer indicating if the element can take input focus (is focusable), if it should participate to sequential keyboard navigation, and if so, at what position. It can take several values:

                          -a negative value means that the element should be focusable, but should not be reachable via sequential keyboard navigation;


                          -0 means that the element should be focusable and reachable via sequential keyboard navigation, but its relative order is defined by the platform convention;


                          -a positive value means should be focusable and reachable via sequential keyboard navigation; its relative order is defined by the value of the attribute: the sequential follow the increasing number of the tabindex. If several elements share the same tabindex, their relative order follows their relative position in the document.




                        from : Mozilla Devlopper Network






                        share|improve this answer




























                          5














                          Just to understand better how tabindex elements works to complete accepted answer :




                          The tabindex global attribute is an integer indicating if the element can take input focus (is focusable), if it should participate to sequential keyboard navigation, and if so, at what position. It can take several values:

                            -a negative value means that the element should be focusable, but should not be reachable via sequential keyboard navigation;


                            -0 means that the element should be focusable and reachable via sequential keyboard navigation, but its relative order is defined by the platform convention;


                            -a positive value means should be focusable and reachable via sequential keyboard navigation; its relative order is defined by the value of the attribute: the sequential follow the increasing number of the tabindex. If several elements share the same tabindex, their relative order follows their relative position in the document.




                          from : Mozilla Devlopper Network






                          share|improve this answer


























                            5












                            5








                            5






                            Just to understand better how tabindex elements works to complete accepted answer :




                            The tabindex global attribute is an integer indicating if the element can take input focus (is focusable), if it should participate to sequential keyboard navigation, and if so, at what position. It can take several values:

                              -a negative value means that the element should be focusable, but should not be reachable via sequential keyboard navigation;


                              -0 means that the element should be focusable and reachable via sequential keyboard navigation, but its relative order is defined by the platform convention;


                              -a positive value means should be focusable and reachable via sequential keyboard navigation; its relative order is defined by the value of the attribute: the sequential follow the increasing number of the tabindex. If several elements share the same tabindex, their relative order follows their relative position in the document.




                            from : Mozilla Devlopper Network






                            share|improve this answer














                            Just to understand better how tabindex elements works to complete accepted answer :




                            The tabindex global attribute is an integer indicating if the element can take input focus (is focusable), if it should participate to sequential keyboard navigation, and if so, at what position. It can take several values:

                              -a negative value means that the element should be focusable, but should not be reachable via sequential keyboard navigation;


                              -0 means that the element should be focusable and reachable via sequential keyboard navigation, but its relative order is defined by the platform convention;


                              -a positive value means should be focusable and reachable via sequential keyboard navigation; its relative order is defined by the value of the attribute: the sequential follow the increasing number of the tabindex. If several elements share the same tabindex, their relative order follows their relative position in the document.




                            from : Mozilla Devlopper Network







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jun 5 '16 at 18:18

























                            answered Jun 5 '16 at 18:05









                            Sébastien Garcia-Roméo

                            1,02111021




                            1,02111021























                                3














                                Based on @pymarco answer I wrote this solution, it's not perfect but solves the select2 focus problem and maintain tab sequence working inside modal



                                    $.fn.modal.Constructor.prototype.enforceFocus = function () {
                                $(document)
                                .off('focusin.bs.modal') // guard against infinite focus loop
                                .on('focusin.bs.modal', $.proxy(function (e) {
                                if (this.$element[0] !== e.target && !this.$element.has(e.target).length && !$(e.target).closest('.select2-dropdown').length) {
                                this.$element.trigger('focus')
                                }
                                }, this))
                                }





                                share|improve this answer


























                                  3














                                  Based on @pymarco answer I wrote this solution, it's not perfect but solves the select2 focus problem and maintain tab sequence working inside modal



                                      $.fn.modal.Constructor.prototype.enforceFocus = function () {
                                  $(document)
                                  .off('focusin.bs.modal') // guard against infinite focus loop
                                  .on('focusin.bs.modal', $.proxy(function (e) {
                                  if (this.$element[0] !== e.target && !this.$element.has(e.target).length && !$(e.target).closest('.select2-dropdown').length) {
                                  this.$element.trigger('focus')
                                  }
                                  }, this))
                                  }





                                  share|improve this answer
























                                    3












                                    3








                                    3






                                    Based on @pymarco answer I wrote this solution, it's not perfect but solves the select2 focus problem and maintain tab sequence working inside modal



                                        $.fn.modal.Constructor.prototype.enforceFocus = function () {
                                    $(document)
                                    .off('focusin.bs.modal') // guard against infinite focus loop
                                    .on('focusin.bs.modal', $.proxy(function (e) {
                                    if (this.$element[0] !== e.target && !this.$element.has(e.target).length && !$(e.target).closest('.select2-dropdown').length) {
                                    this.$element.trigger('focus')
                                    }
                                    }, this))
                                    }





                                    share|improve this answer












                                    Based on @pymarco answer I wrote this solution, it's not perfect but solves the select2 focus problem and maintain tab sequence working inside modal



                                        $.fn.modal.Constructor.prototype.enforceFocus = function () {
                                    $(document)
                                    .off('focusin.bs.modal') // guard against infinite focus loop
                                    .on('focusin.bs.modal', $.proxy(function (e) {
                                    if (this.$element[0] !== e.target && !this.$element.has(e.target).length && !$(e.target).closest('.select2-dropdown').length) {
                                    this.$element.trigger('focus')
                                    }
                                    }, this))
                                    }






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Mar 7 at 13:45









                                    Angelo Cavalet

                                    7119




                                    7119























                                        2














                                        If you use jquery mobile popup you must rewrite _handleDocumentFocusIn function:






                                        $.mobile.popup.prototype._handleDocumentFocusIn = function(e) {
                                        if ($(e.target).closest('.select2-dropdown').length) return true;
                                        }








                                        share|improve this answer





















                                        • I'm using jQuery Mobile and have embedded Select2 in a popup. The code you have provided works good on a PC browser. But when I run this code on a mobile browser, the popup closes and reopens when any value is selected. Can you suggest any changes I need to make?
                                          – Sahil Khanna
                                          Feb 20 '16 at 7:03
















                                        2














                                        If you use jquery mobile popup you must rewrite _handleDocumentFocusIn function:






                                        $.mobile.popup.prototype._handleDocumentFocusIn = function(e) {
                                        if ($(e.target).closest('.select2-dropdown').length) return true;
                                        }








                                        share|improve this answer





















                                        • I'm using jQuery Mobile and have embedded Select2 in a popup. The code you have provided works good on a PC browser. But when I run this code on a mobile browser, the popup closes and reopens when any value is selected. Can you suggest any changes I need to make?
                                          – Sahil Khanna
                                          Feb 20 '16 at 7:03














                                        2












                                        2








                                        2






                                        If you use jquery mobile popup you must rewrite _handleDocumentFocusIn function:






                                        $.mobile.popup.prototype._handleDocumentFocusIn = function(e) {
                                        if ($(e.target).closest('.select2-dropdown').length) return true;
                                        }








                                        share|improve this answer












                                        If you use jquery mobile popup you must rewrite _handleDocumentFocusIn function:






                                        $.mobile.popup.prototype._handleDocumentFocusIn = function(e) {
                                        if ($(e.target).closest('.select2-dropdown').length) return true;
                                        }








                                        $.mobile.popup.prototype._handleDocumentFocusIn = function(e) {
                                        if ($(e.target).closest('.select2-dropdown').length) return true;
                                        }





                                        $.mobile.popup.prototype._handleDocumentFocusIn = function(e) {
                                        if ($(e.target).closest('.select2-dropdown').length) return true;
                                        }






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Apr 16 '15 at 14:57









                                        Dr. Clò Luca

                                        213




                                        213












                                        • I'm using jQuery Mobile and have embedded Select2 in a popup. The code you have provided works good on a PC browser. But when I run this code on a mobile browser, the popup closes and reopens when any value is selected. Can you suggest any changes I need to make?
                                          – Sahil Khanna
                                          Feb 20 '16 at 7:03


















                                        • I'm using jQuery Mobile and have embedded Select2 in a popup. The code you have provided works good on a PC browser. But when I run this code on a mobile browser, the popup closes and reopens when any value is selected. Can you suggest any changes I need to make?
                                          – Sahil Khanna
                                          Feb 20 '16 at 7:03
















                                        I'm using jQuery Mobile and have embedded Select2 in a popup. The code you have provided works good on a PC browser. But when I run this code on a mobile browser, the popup closes and reopens when any value is selected. Can you suggest any changes I need to make?
                                        – Sahil Khanna
                                        Feb 20 '16 at 7:03




                                        I'm using jQuery Mobile and have embedded Select2 in a popup. The code you have provided works good on a PC browser. But when I run this code on a mobile browser, the popup closes and reopens when any value is selected. Can you suggest any changes I need to make?
                                        – Sahil Khanna
                                        Feb 20 '16 at 7:03











                                        2














                                        If you have a problem with the iPad keyboard which hide the bootstrap modal while clicking on the select2 input, you can resolve this by adding the following rule after the initialization of the select2 input :



                                        if (navigator.userAgent.match(/iPhone|iPad|iPod/i)) {
                                        var styleEl = document.createElement('style'), styleSheet;
                                        document.head.appendChild(styleEl);
                                        styleSheet = styleEl.sheet;
                                        styleSheet.insertRule(".modal { position:absolute; bottom:auto; }", 0);
                                        document.body.scrollTop = 0; // Only for Safari
                                        }


                                        Taken from https://github.com/angular-ui/bootstrap/issues/1812#issuecomment-135119475



                                        EDIT: If your options are not shown properly, you need to use the dropdownParent attribute when initializing select2 :



                                        $(".select2").select2({
                                        dropdownParent: $("#YOURMODALID")
                                        });


                                        Good luck (:






                                        share|improve this answer




























                                          2














                                          If you have a problem with the iPad keyboard which hide the bootstrap modal while clicking on the select2 input, you can resolve this by adding the following rule after the initialization of the select2 input :



                                          if (navigator.userAgent.match(/iPhone|iPad|iPod/i)) {
                                          var styleEl = document.createElement('style'), styleSheet;
                                          document.head.appendChild(styleEl);
                                          styleSheet = styleEl.sheet;
                                          styleSheet.insertRule(".modal { position:absolute; bottom:auto; }", 0);
                                          document.body.scrollTop = 0; // Only for Safari
                                          }


                                          Taken from https://github.com/angular-ui/bootstrap/issues/1812#issuecomment-135119475



                                          EDIT: If your options are not shown properly, you need to use the dropdownParent attribute when initializing select2 :



                                          $(".select2").select2({
                                          dropdownParent: $("#YOURMODALID")
                                          });


                                          Good luck (:






                                          share|improve this answer


























                                            2












                                            2








                                            2






                                            If you have a problem with the iPad keyboard which hide the bootstrap modal while clicking on the select2 input, you can resolve this by adding the following rule after the initialization of the select2 input :



                                            if (navigator.userAgent.match(/iPhone|iPad|iPod/i)) {
                                            var styleEl = document.createElement('style'), styleSheet;
                                            document.head.appendChild(styleEl);
                                            styleSheet = styleEl.sheet;
                                            styleSheet.insertRule(".modal { position:absolute; bottom:auto; }", 0);
                                            document.body.scrollTop = 0; // Only for Safari
                                            }


                                            Taken from https://github.com/angular-ui/bootstrap/issues/1812#issuecomment-135119475



                                            EDIT: If your options are not shown properly, you need to use the dropdownParent attribute when initializing select2 :



                                            $(".select2").select2({
                                            dropdownParent: $("#YOURMODALID")
                                            });


                                            Good luck (:






                                            share|improve this answer














                                            If you have a problem with the iPad keyboard which hide the bootstrap modal while clicking on the select2 input, you can resolve this by adding the following rule after the initialization of the select2 input :



                                            if (navigator.userAgent.match(/iPhone|iPad|iPod/i)) {
                                            var styleEl = document.createElement('style'), styleSheet;
                                            document.head.appendChild(styleEl);
                                            styleSheet = styleEl.sheet;
                                            styleSheet.insertRule(".modal { position:absolute; bottom:auto; }", 0);
                                            document.body.scrollTop = 0; // Only for Safari
                                            }


                                            Taken from https://github.com/angular-ui/bootstrap/issues/1812#issuecomment-135119475



                                            EDIT: If your options are not shown properly, you need to use the dropdownParent attribute when initializing select2 :



                                            $(".select2").select2({
                                            dropdownParent: $("#YOURMODALID")
                                            });


                                            Good luck (:







                                            share|improve this answer














                                            share|improve this answer



                                            share|improve this answer








                                            edited Feb 5 at 14:08

























                                            answered Feb 5 at 13:45









                                            Gangai Johann

                                            617712




                                            617712























                                                2














                                                Okay, I know I'm late to the party. But let me share with you what worked for me.
                                                The tabindex and z-index solutions did not work for me.



                                                Setting the parent of the select element worked as per the common problems listed on select2 site.






                                                share|improve this answer


























                                                  2














                                                  Okay, I know I'm late to the party. But let me share with you what worked for me.
                                                  The tabindex and z-index solutions did not work for me.



                                                  Setting the parent of the select element worked as per the common problems listed on select2 site.






                                                  share|improve this answer
























                                                    2












                                                    2








                                                    2






                                                    Okay, I know I'm late to the party. But let me share with you what worked for me.
                                                    The tabindex and z-index solutions did not work for me.



                                                    Setting the parent of the select element worked as per the common problems listed on select2 site.






                                                    share|improve this answer












                                                    Okay, I know I'm late to the party. But let me share with you what worked for me.
                                                    The tabindex and z-index solutions did not work for me.



                                                    Setting the parent of the select element worked as per the common problems listed on select2 site.







                                                    share|improve this answer












                                                    share|improve this answer



                                                    share|improve this answer










                                                    answered Jul 2 at 11:30









                                                    joker

                                                    1,29821621




                                                    1,29821621























                                                        2














                                                        This Problem Sloved Working For Me Single Jquery Function



                                                        $('#myModal .select2').each(function() {  
                                                        var $p = $(this).parent();
                                                        $(this).select2({
                                                        dropdownParent: $p
                                                        });
                                                        });





                                                        share|improve this answer


























                                                          2














                                                          This Problem Sloved Working For Me Single Jquery Function



                                                          $('#myModal .select2').each(function() {  
                                                          var $p = $(this).parent();
                                                          $(this).select2({
                                                          dropdownParent: $p
                                                          });
                                                          });





                                                          share|improve this answer
























                                                            2












                                                            2








                                                            2






                                                            This Problem Sloved Working For Me Single Jquery Function



                                                            $('#myModal .select2').each(function() {  
                                                            var $p = $(this).parent();
                                                            $(this).select2({
                                                            dropdownParent: $p
                                                            });
                                                            });





                                                            share|improve this answer












                                                            This Problem Sloved Working For Me Single Jquery Function



                                                            $('#myModal .select2').each(function() {  
                                                            var $p = $(this).parent();
                                                            $(this).select2({
                                                            dropdownParent: $p
                                                            });
                                                            });






                                                            share|improve this answer












                                                            share|improve this answer



                                                            share|improve this answer










                                                            answered Oct 9 at 5:50









                                                            Vinoth Smart

                                                            1129




                                                            1129























                                                                1














                                                                I have the same problem with the select2 in bootstrap modal, and the solution was to remove the overflow-y: auto; and overflow: hidden; from .modal-open and .modal classes



                                                                Here is the example of using jQuery to remove the overflow-y:



                                                                $('.modal').css('overflow-y','visible');
                                                                $('.modal').css('overflow','visible');





                                                                share|improve this answer


























                                                                  1














                                                                  I have the same problem with the select2 in bootstrap modal, and the solution was to remove the overflow-y: auto; and overflow: hidden; from .modal-open and .modal classes



                                                                  Here is the example of using jQuery to remove the overflow-y:



                                                                  $('.modal').css('overflow-y','visible');
                                                                  $('.modal').css('overflow','visible');





                                                                  share|improve this answer
























                                                                    1












                                                                    1








                                                                    1






                                                                    I have the same problem with the select2 in bootstrap modal, and the solution was to remove the overflow-y: auto; and overflow: hidden; from .modal-open and .modal classes



                                                                    Here is the example of using jQuery to remove the overflow-y:



                                                                    $('.modal').css('overflow-y','visible');
                                                                    $('.modal').css('overflow','visible');





                                                                    share|improve this answer












                                                                    I have the same problem with the select2 in bootstrap modal, and the solution was to remove the overflow-y: auto; and overflow: hidden; from .modal-open and .modal classes



                                                                    Here is the example of using jQuery to remove the overflow-y:



                                                                    $('.modal').css('overflow-y','visible');
                                                                    $('.modal').css('overflow','visible');






                                                                    share|improve this answer












                                                                    share|improve this answer



                                                                    share|improve this answer










                                                                    answered Apr 28 '17 at 10:46









                                                                    raBne

                                                                    1,68912032




                                                                    1,68912032























                                                                        1














                                                                        i had this problem before , i am using yii2 and i solved it this way



                                                                        $.fn.modal.Constructor.prototype.enforceFocus = $.noop;





                                                                        share|improve this answer


























                                                                          1














                                                                          i had this problem before , i am using yii2 and i solved it this way



                                                                          $.fn.modal.Constructor.prototype.enforceFocus = $.noop;





                                                                          share|improve this answer
























                                                                            1












                                                                            1








                                                                            1






                                                                            i had this problem before , i am using yii2 and i solved it this way



                                                                            $.fn.modal.Constructor.prototype.enforceFocus = $.noop;





                                                                            share|improve this answer












                                                                            i had this problem before , i am using yii2 and i solved it this way



                                                                            $.fn.modal.Constructor.prototype.enforceFocus = $.noop;






                                                                            share|improve this answer












                                                                            share|improve this answer



                                                                            share|improve this answer










                                                                            answered May 16 '17 at 16:11









                                                                            yousef

                                                                            40958




                                                                            40958























                                                                                1














                                                                                $("#IlceId").select2({
                                                                                allowClear: true,
                                                                                multiple: false,
                                                                                dropdownParent: $("#IlceId").parent(),
                                                                                escapeMarkup: function (m) {
                                                                                return m;
                                                                                },
                                                                                });


                                                                                this code is working. Thank you.






                                                                                share|improve this answer




























                                                                                  1














                                                                                  $("#IlceId").select2({
                                                                                  allowClear: true,
                                                                                  multiple: false,
                                                                                  dropdownParent: $("#IlceId").parent(),
                                                                                  escapeMarkup: function (m) {
                                                                                  return m;
                                                                                  },
                                                                                  });


                                                                                  this code is working. Thank you.






                                                                                  share|improve this answer


























                                                                                    1












                                                                                    1








                                                                                    1






                                                                                    $("#IlceId").select2({
                                                                                    allowClear: true,
                                                                                    multiple: false,
                                                                                    dropdownParent: $("#IlceId").parent(),
                                                                                    escapeMarkup: function (m) {
                                                                                    return m;
                                                                                    },
                                                                                    });


                                                                                    this code is working. Thank you.






                                                                                    share|improve this answer














                                                                                    $("#IlceId").select2({
                                                                                    allowClear: true,
                                                                                    multiple: false,
                                                                                    dropdownParent: $("#IlceId").parent(),
                                                                                    escapeMarkup: function (m) {
                                                                                    return m;
                                                                                    },
                                                                                    });


                                                                                    this code is working. Thank you.







                                                                                    share|improve this answer














                                                                                    share|improve this answer



                                                                                    share|improve this answer








                                                                                    edited Dec 19 '17 at 8:18









                                                                                    Buddy

                                                                                    8,56952951




                                                                                    8,56952951










                                                                                    answered Dec 19 '17 at 8:16









                                                                                    ramazan polat

                                                                                    38325




                                                                                    38325























                                                                                        1














                                                                                        I solved this generally in my project by overloading the select2-function. Now it will check if there is no dropdownParent and if the function is called on an element that has a parent of the type div.modal. If so, it will add that modal as the parent for the dropdown.



                                                                                        This way, you don't have to specify it every time you create a select2-input-box.



                                                                                        (function(){
                                                                                        var oldSelect2 = jQuery.fn.select2;
                                                                                        jQuery.fn.select2 = function() {
                                                                                        const modalParent = jQuery(this).parents('div.modal').first();
                                                                                        if (arguments.length === 0 && modalParent.length > 0) {
                                                                                        arguments = [{dropdownParent: modalParent}];
                                                                                        } else if (arguments.length === 1
                                                                                        && typeof arguments[0] === 'object'
                                                                                        && typeof arguments[0].dropdownParent === 'undefined'
                                                                                        && modalParent.length > 0) {
                                                                                        arguments[0].dropdownParent = modalParent;
                                                                                        }
                                                                                        return oldSelect2.apply(this,arguments);
                                                                                        };
                                                                                        })();





                                                                                        share|improve this answer




























                                                                                          1














                                                                                          I solved this generally in my project by overloading the select2-function. Now it will check if there is no dropdownParent and if the function is called on an element that has a parent of the type div.modal. If so, it will add that modal as the parent for the dropdown.



                                                                                          This way, you don't have to specify it every time you create a select2-input-box.



                                                                                          (function(){
                                                                                          var oldSelect2 = jQuery.fn.select2;
                                                                                          jQuery.fn.select2 = function() {
                                                                                          const modalParent = jQuery(this).parents('div.modal').first();
                                                                                          if (arguments.length === 0 && modalParent.length > 0) {
                                                                                          arguments = [{dropdownParent: modalParent}];
                                                                                          } else if (arguments.length === 1
                                                                                          && typeof arguments[0] === 'object'
                                                                                          && typeof arguments[0].dropdownParent === 'undefined'
                                                                                          && modalParent.length > 0) {
                                                                                          arguments[0].dropdownParent = modalParent;
                                                                                          }
                                                                                          return oldSelect2.apply(this,arguments);
                                                                                          };
                                                                                          })();





                                                                                          share|improve this answer


























                                                                                            1












                                                                                            1








                                                                                            1






                                                                                            I solved this generally in my project by overloading the select2-function. Now it will check if there is no dropdownParent and if the function is called on an element that has a parent of the type div.modal. If so, it will add that modal as the parent for the dropdown.



                                                                                            This way, you don't have to specify it every time you create a select2-input-box.



                                                                                            (function(){
                                                                                            var oldSelect2 = jQuery.fn.select2;
                                                                                            jQuery.fn.select2 = function() {
                                                                                            const modalParent = jQuery(this).parents('div.modal').first();
                                                                                            if (arguments.length === 0 && modalParent.length > 0) {
                                                                                            arguments = [{dropdownParent: modalParent}];
                                                                                            } else if (arguments.length === 1
                                                                                            && typeof arguments[0] === 'object'
                                                                                            && typeof arguments[0].dropdownParent === 'undefined'
                                                                                            && modalParent.length > 0) {
                                                                                            arguments[0].dropdownParent = modalParent;
                                                                                            }
                                                                                            return oldSelect2.apply(this,arguments);
                                                                                            };
                                                                                            })();





                                                                                            share|improve this answer














                                                                                            I solved this generally in my project by overloading the select2-function. Now it will check if there is no dropdownParent and if the function is called on an element that has a parent of the type div.modal. If so, it will add that modal as the parent for the dropdown.



                                                                                            This way, you don't have to specify it every time you create a select2-input-box.



                                                                                            (function(){
                                                                                            var oldSelect2 = jQuery.fn.select2;
                                                                                            jQuery.fn.select2 = function() {
                                                                                            const modalParent = jQuery(this).parents('div.modal').first();
                                                                                            if (arguments.length === 0 && modalParent.length > 0) {
                                                                                            arguments = [{dropdownParent: modalParent}];
                                                                                            } else if (arguments.length === 1
                                                                                            && typeof arguments[0] === 'object'
                                                                                            && typeof arguments[0].dropdownParent === 'undefined'
                                                                                            && modalParent.length > 0) {
                                                                                            arguments[0].dropdownParent = modalParent;
                                                                                            }
                                                                                            return oldSelect2.apply(this,arguments);
                                                                                            };
                                                                                            })();






                                                                                            share|improve this answer














                                                                                            share|improve this answer



                                                                                            share|improve this answer








                                                                                            edited Dec 27 at 14:17

























                                                                                            answered Sep 27 at 13:46









                                                                                            Zombaya

                                                                                            1,8241624




                                                                                            1,8241624























                                                                                                0














                                                                                                I just get it working by including select2.min.css



                                                                                                Try iy out



                                                                                                My modal html of bootstrap 3 is



                                                                                                <div id="changeTransportUserRequestPopup" class="modal fade" role="dialog">
                                                                                                <div class="modal-dialog" style="width: 40%!important; ">
                                                                                                <div class="modal-content">
                                                                                                <div class="modal-header">
                                                                                                <button type="button" class="close" data-dismiss="modal">&times;</button>
                                                                                                <h3>Warning</h3>
                                                                                                </div>
                                                                                                <div class="modal-body" id="changeTransportUserRequestPopupBody">
                                                                                                <select id="cbTransport" class="js-example-basic-single" name="cbTransport" style="width:100%!important;"></select>
                                                                                                </div>
                                                                                                <div class="modal-footer">
                                                                                                <button id="noPost" class="btn btn-default" name="postConfirm" value="false" data-dismiss="modal">Cancel</button>
                                                                                                <button type="submit" id="yesChangeTransportPost" class="btn btn-success" name="yesChangeTransportPost" value="true" data-dismiss="modal">Yes</button>
                                                                                                </div>
                                                                                                </div>
                                                                                                </div>
                                                                                                </div>





                                                                                                share|improve this answer


























                                                                                                  0














                                                                                                  I just get it working by including select2.min.css



                                                                                                  Try iy out



                                                                                                  My modal html of bootstrap 3 is



                                                                                                  <div id="changeTransportUserRequestPopup" class="modal fade" role="dialog">
                                                                                                  <div class="modal-dialog" style="width: 40%!important; ">
                                                                                                  <div class="modal-content">
                                                                                                  <div class="modal-header">
                                                                                                  <button type="button" class="close" data-dismiss="modal">&times;</button>
                                                                                                  <h3>Warning</h3>
                                                                                                  </div>
                                                                                                  <div class="modal-body" id="changeTransportUserRequestPopupBody">
                                                                                                  <select id="cbTransport" class="js-example-basic-single" name="cbTransport" style="width:100%!important;"></select>
                                                                                                  </div>
                                                                                                  <div class="modal-footer">
                                                                                                  <button id="noPost" class="btn btn-default" name="postConfirm" value="false" data-dismiss="modal">Cancel</button>
                                                                                                  <button type="submit" id="yesChangeTransportPost" class="btn btn-success" name="yesChangeTransportPost" value="true" data-dismiss="modal">Yes</button>
                                                                                                  </div>
                                                                                                  </div>
                                                                                                  </div>
                                                                                                  </div>





                                                                                                  share|improve this answer
























                                                                                                    0












                                                                                                    0








                                                                                                    0






                                                                                                    I just get it working by including select2.min.css



                                                                                                    Try iy out



                                                                                                    My modal html of bootstrap 3 is



                                                                                                    <div id="changeTransportUserRequestPopup" class="modal fade" role="dialog">
                                                                                                    <div class="modal-dialog" style="width: 40%!important; ">
                                                                                                    <div class="modal-content">
                                                                                                    <div class="modal-header">
                                                                                                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                                                                                                    <h3>Warning</h3>
                                                                                                    </div>
                                                                                                    <div class="modal-body" id="changeTransportUserRequestPopupBody">
                                                                                                    <select id="cbTransport" class="js-example-basic-single" name="cbTransport" style="width:100%!important;"></select>
                                                                                                    </div>
                                                                                                    <div class="modal-footer">
                                                                                                    <button id="noPost" class="btn btn-default" name="postConfirm" value="false" data-dismiss="modal">Cancel</button>
                                                                                                    <button type="submit" id="yesChangeTransportPost" class="btn btn-success" name="yesChangeTransportPost" value="true" data-dismiss="modal">Yes</button>
                                                                                                    </div>
                                                                                                    </div>
                                                                                                    </div>
                                                                                                    </div>





                                                                                                    share|improve this answer












                                                                                                    I just get it working by including select2.min.css



                                                                                                    Try iy out



                                                                                                    My modal html of bootstrap 3 is



                                                                                                    <div id="changeTransportUserRequestPopup" class="modal fade" role="dialog">
                                                                                                    <div class="modal-dialog" style="width: 40%!important; ">
                                                                                                    <div class="modal-content">
                                                                                                    <div class="modal-header">
                                                                                                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                                                                                                    <h3>Warning</h3>
                                                                                                    </div>
                                                                                                    <div class="modal-body" id="changeTransportUserRequestPopupBody">
                                                                                                    <select id="cbTransport" class="js-example-basic-single" name="cbTransport" style="width:100%!important;"></select>
                                                                                                    </div>
                                                                                                    <div class="modal-footer">
                                                                                                    <button id="noPost" class="btn btn-default" name="postConfirm" value="false" data-dismiss="modal">Cancel</button>
                                                                                                    <button type="submit" id="yesChangeTransportPost" class="btn btn-success" name="yesChangeTransportPost" value="true" data-dismiss="modal">Yes</button>
                                                                                                    </div>
                                                                                                    </div>
                                                                                                    </div>
                                                                                                    </div>






                                                                                                    share|improve this answer












                                                                                                    share|improve this answer



                                                                                                    share|improve this answer










                                                                                                    answered Sep 6 at 1:39









                                                                                                    Academy of Programmer

                                                                                                    17.4k53228377




                                                                                                    17.4k53228377























                                                                                                        0














                                                                                                        I had a semi-related issue in an application so I'll put in my 2c.



                                                                                                        I have multiple modals with forms containing select2 widgets. Opening modal A, then another modal inside modal A, would cause select2 widgets inside modal B to disappear and fail to initialize.



                                                                                                        Each of these modals were loading the forms via ajax.



                                                                                                        The solution was to remove the forms from the dom when closing a modal.



                                                                                                        $(document).on('hidden.bs.modal', '.modal', function(e) {
                                                                                                        // make sure we don't leave any select2 widgets around
                                                                                                        $(this).find('.my-form').remove();
                                                                                                        });





                                                                                                        share|improve this answer


























                                                                                                          0














                                                                                                          I had a semi-related issue in an application so I'll put in my 2c.



                                                                                                          I have multiple modals with forms containing select2 widgets. Opening modal A, then another modal inside modal A, would cause select2 widgets inside modal B to disappear and fail to initialize.



                                                                                                          Each of these modals were loading the forms via ajax.



                                                                                                          The solution was to remove the forms from the dom when closing a modal.



                                                                                                          $(document).on('hidden.bs.modal', '.modal', function(e) {
                                                                                                          // make sure we don't leave any select2 widgets around
                                                                                                          $(this).find('.my-form').remove();
                                                                                                          });





                                                                                                          share|improve this answer
























                                                                                                            0












                                                                                                            0








                                                                                                            0






                                                                                                            I had a semi-related issue in an application so I'll put in my 2c.



                                                                                                            I have multiple modals with forms containing select2 widgets. Opening modal A, then another modal inside modal A, would cause select2 widgets inside modal B to disappear and fail to initialize.



                                                                                                            Each of these modals were loading the forms via ajax.



                                                                                                            The solution was to remove the forms from the dom when closing a modal.



                                                                                                            $(document).on('hidden.bs.modal', '.modal', function(e) {
                                                                                                            // make sure we don't leave any select2 widgets around
                                                                                                            $(this).find('.my-form').remove();
                                                                                                            });





                                                                                                            share|improve this answer












                                                                                                            I had a semi-related issue in an application so I'll put in my 2c.



                                                                                                            I have multiple modals with forms containing select2 widgets. Opening modal A, then another modal inside modal A, would cause select2 widgets inside modal B to disappear and fail to initialize.



                                                                                                            Each of these modals were loading the forms via ajax.



                                                                                                            The solution was to remove the forms from the dom when closing a modal.



                                                                                                            $(document).on('hidden.bs.modal', '.modal', function(e) {
                                                                                                            // make sure we don't leave any select2 widgets around
                                                                                                            $(this).find('.my-form').remove();
                                                                                                            });






                                                                                                            share|improve this answer












                                                                                                            share|improve this answer



                                                                                                            share|improve this answer










                                                                                                            answered Dec 2 at 20:59









                                                                                                            glyph

                                                                                                            8151229




                                                                                                            8151229

















                                                                                                                protected by Community Jan 6 '17 at 12:09



                                                                                                                Thank you for your interest in this question.
                                                                                                                Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                                                                                Would you like to answer one of these unanswered questions instead?



                                                                                                                Popular posts from this blog

                                                                                                                Monofisismo

                                                                                                                Angular Downloading a file using contenturl with Basic Authentication

                                                                                                                Olmecas