Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap-select not working inside modal

I am implementing this selectpicker for my website. I encountered an issue that an element that has the selectpicker class it doesn't show on my DOM.

here is the code

<div id="modal-container"  class="modal fade hidden-print" tabindex="-1" role="dialog" data-backdrop="static" data-keyboard="false">
    <div class="modal-dialog">
        <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">Add New Experience</h4>
    </div>

        <div id="addExperience" class="modal-body">
            <div class="form-horizontal">
                <div class="form-group">
                                <div class="col-md-12">
                                    <select class="user-form selectpicker show-tick form-control" data-live-search="true">
                                        <option>A</option>
                                    </select>
                                </div>
                            </div>
            </div>

        </div>
    </div>
</div>

please help me get in track with my development.

Thanks!

like image 401
Terence Avatar asked Jul 14 '26 06:07

Terence


2 Answers

You have to reinitialize your Bootstrap-Select (.selectpicker) after loading your modal. The reason why this is happening is, your modal is a newly created element and your selectpicker is not yet initialized on it.

Here's my code:

// Create
$(document).on('click', '.add', function()
{
    $.ajax({
        type: 'GET',
        url: 'suppliers/create',
        dataType: 'json',
        success: function(data)
        {
            modal.find('.modal-title').html(data.title);
            modal.find('.modal-body').html(data.body);
            $('.selectpicker').selectpicker();
        }
    });
    modal.modal('show');
});

Thanks very much to Cerlin Boss for this solution!

like image 94
sylenix Avatar answered Jul 20 '26 09:07

sylenix


You need to add the below required resources in your plunker example

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
  </script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.1/css/bootstrap-select.min.css">

<!-- Latest compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.1/js/bootstrap-select.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.1/js/i18n/defaults-en_US.js"></script>

and things work fine :)

Let me know in case the actual code still differs from the plunker.

Example : https://plnkr.co/edit/4gJBAu3NKUQp1HMUbrK9?p=preview

like image 39
Deep Avatar answered Jul 20 '26 08:07

Deep



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!