Select2 not working inside bootstrap modal
Asked Answered
C

15

12

I am trying to use select2 inside bootstrap modal but it is not getting the focus automatically as well as down and up arrows are not working for the populated list.

The same select2 works when I put it outside the modal popup.

When I searched, I found many are facing this same problem and found this post

Select2 doesn't work when embedded in a bootstrap modal

I implemented both the solutions from it

  1. Removed tabindex from modal popup.
  2. Commented code of enforceFocus function in modal.js file.

But it is still not working! Any idea what I could still be missing?

Edit1

It works on firefox when tabindex is removed from the modal div but not with IE9

Edit2

I found that removing tabindex is actually not getting recognized by IE9 because I can still hide the popup by escape key in IE but not in Firefox.

Coulson answered 30/12, 2013 at 8:25 Comment(0)
T
19

Use bellow code. This will solve your bug.

$('select').select2({
    dropdownParent: $('#my_amazing_modal')
});
Tautologism answered 19/10, 2017 at 7:30 Comment(1)
If you using this for multiple modals you can use dropdownParent: $(".modal-body , .modal-body1")Mir
A
16

place this somewhere in your JS:

   //fix modal force focus
   $.fn.modal.Constructor.prototype.enforceFocus = function () {
      var that = this;
      $(document).on('focusin.modal', function (e) {
         if ($(e.target).hasClass('select2-input')) {
            return true;
         }

         if (that.$element[0] !== e.target && !that.$element.has(e.target).length) {
            that.$element.focus();
         }
      });
   };
Aimee answered 24/2, 2014 at 14:57 Comment(0)
T
6

I changed:

<div class="modal fade bd-example-modal-lg"  tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">

to the following:

<div class="modal fade bd-example-modal-lg" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">

That is, removing the attribute tabindex="-1" from the element.

Tippets answered 3/8, 2017 at 6:8 Comment(1)
Are there any downsides to this approach?Collimate
P
5

Try this

<div class="col-sm-8" id="select">
    <select type="text" class="form-control" id="helloselect" data-width="100%" name="helloselect">
        <option>Hello</option>
        <option>Hola</option>
        <option>Hallo</option>
    </select>
</div>

and the script

$("#helloselect").select2({dropdownParent: $("#select")});
Phallic answered 3/8, 2017 at 16:0 Comment(0)
B
3

I was searching for it and came across this one:

Worked for me on document ready for all select2

 $('select:not(.normal)').each(function () {
            $(this).select2({
                dropdownParent: $(this).parent()
            });
        });

Taken from: https://github.com/select2/select2-bootstrap-theme/issues/41

Buchanan answered 6/11, 2020 at 13:44 Comment(1)
This worked. I added an id to the main container and used the same as the answer. dropdownParent: $('#new_appended_container')Politico
C
2

I tried to set z-index for select2 dropdown option, and it worked for me. Here is what I did:

.select2-container.select2-container--default.select2-container--open  {
  z-index: 5000;
}
Companionway answered 17/5, 2017 at 10:14 Comment(0)
O
2
$('#dropdownid').select2({
  dropdownParent: $('#modalid')
 });

Above code works for me, try this.

Organdy answered 24/10, 2019 at 11:12 Comment(0)
U
1

this will do the trick, and it worked for me

$.fn.modal.Constructor.prototype.enforceFocus = $.noop;
Unmixed answered 16/5, 2017 at 16:9 Comment(0)
A
1

the attribute tabindex='-1' in bootstrap modal prevents any element outside modal being reachable or focused by tab key. It is useful for limit tabs only on modal, for accessibility. But elements created by selec2 are children of document. You need to set dropdownParent to your modal

$(document).ready(function() {

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

    $('[data-toggle=offcanvas]').click(function() {
      $('.row-offcanvas').toggleClass('active');
    });
  


});
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>JS Bin</title>
  <link href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css" rel="stylesheet" />
  <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.2/css/bootstrap.css" rel="stylesheet" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.2.0/js/tether.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.2/js/bootstrap.js"></script>

</head>
<body>
  <a href="" data-target="#myModal" data-toggle="modal">Convert to popup</a>
  <div class="modal fade" id="myModal" tabindex="-1" role="dialog">
    <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"><i class="fa fa-close"></i></span>
            <span class="sr-only">Close</span>
          </button>
          <h4 class="modal-title" id="myModalLabel">Bootsrap4 Modal Label</h4>
        </div>
        <div class="modal-body">
		  <div id="PopupWrapper">
            <select class="form-control">
              <option>red</option>
              <option>blue</option>
              <option>green</option>
              <option>orange</option>
              <option>yellow</option>
            </select>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>

jsbin example

meaning of tabindex=-1

Abisia answered 14/2, 2019 at 20:5 Comment(0)
S
1

Just use data attribute settings: data-dropdown-parent="#bs-modal-id"

<select class="form-control" data-dropdown-parent="#bs-modal-id"></select>
Shampoo answered 9/2, 2022 at 12:1 Comment(1)
easy solution works on laravel 8Skolnik
P
1

I was facing with the same problem I just add the below code and it worked

$(document).ready(function() {
           $('.select2').each(function() {
              $(this).select2({
                  dropdownParent: $(this).parent()
              });
          })
      });
Peacemaker answered 9/12, 2022 at 5:12 Comment(2)
One of the best working answers on this issue. I was facing this problem in bootstrap modal with multiple instances. You are a CHAMP!Buchanan
Furthermore, this one works great and equally good for Single or Multiple Dropdowns in Bootstrap forms with and without Bootstrap Modal!!!Buchanan
T
0

Dear all no need to take tabindex="-1" out there simple CSS solution. Here we go:

.page-body .select2-drop {z-index: 10052;}
.select2-drop-mask {z-index: 10052;}

And you are done. :)

Tessatessellate answered 3/5, 2018 at 19:56 Comment(0)
H
0
$('.select2-with-search').select2({
      placeholder: 'Choose one',
      searchInputPlaceholder: 'Search options',
   dropdownParent: $('.modal')
   });

us this <div class="modal"

<div class="modal fade" id="createModal" role="dialog" aria-hidden="true"  data-backdrop="">
   <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
      <div class="modal-content">

and use this <select class="form-control select2-with-search"

<select class="form-control wd-250 select2-with-search">
                     <option label="Choose one"></option>
                     <option value="Firefox">Firefox</option>
                     <option value="Chrome">Chrome</option>
                     <option value="Safari">Safari</option>
                     <option value="Opera">Opera</option>
                     <option value="Internet Explorer">Internet Explorer</option>
                  </select>
Horal answered 15/3, 2021 at 12:31 Comment(0)
C
0

All answers here didn't work for me, but this one is good:

  $('#modal-window').on('shown.bs.modal', function (e) {
    $("#select-box").select2({
      placeholder: "Select...",
      theme: "bootstrap",
      language: "en"
    });
  })

Wrap it inside

$(document).on('ready turbolinks:load', function() {...}

if you use turbolink.

Compressor answered 16/5, 2021 at 7:4 Comment(0)
N
0

In my case i resolve this calling select2 on action modal show like this:

    window.addEventListener('showModal', event => {
        $('#actionModal').modal('show');
        $('#permissaoList').select2({
            width: '100%'
        });
    });
Natividadnativism answered 27/9, 2022 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.