JavaScript Bootstrap-select with ajax jquery is not working bootstrap select with search,bootstrap select with checkboxes,bootstrap select

I'm using bootstrap-select and everything is working fine,until options of my select tag are filled with data from php file. I have seen each post on this topic, but there is no answer. So here is an example: Style:

<link rel="stylesheet" target='_blank' href="dist/css/bootstrap-select.css"><!-- This is from my path-->

<link rel="stylesheet"target='_blank' href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">

Html:

<!-- This is working -->
<div class="players col col-lg-3">
    <label for='selectPlayer'> Pick the Player: </label>
    <select class='selectpicker' data-style='btn-info'>
        <optgroup id="opt" label='Players'>
            <option>Ronaldo</option>
        </optgroup>
    </select>
</div> <!-- Select Player -->

<!-- This is not working,when using ajax jquery -->
<label for='selectClub'> Tested ajax jquery : </label>
<div id="players1" class="players1" data-container="body"></div> <!-- Jquery-->

Scripts:

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>

<script src="dist/js/bootstrap-select.js"></script> <!-- This is from my path,and must be at the end-->

<script>
/* ---------- References ---------- */
// http://stackoverflow.com/questions/15726411/how-to-use-bootstrap-select
// http://stackoverflow.com/questions/5807565/return-value-from-ajax-call-is-not-shown-in-the-page-source
$(document).ready(function() {
$.ajax({
        type:"GET",
        url:'selectPHP.php',
        data:{},
        success: function (response) {
            alert(response);
            $('#players1').html(response);
            console.log((response));
        },
        error: function () {
            $('#players1').html('There was an error!');
        }
    });

    $('.selectpicker').selectpicker({
        style: 'btn-primary',
        size: 2
    });
});
</script>

php file:

echo "<select class='selectpicker' data-style='btn-primary'>";
echo "<option> data 1 </option>";
echo " </select><!-- select -->";

I'm receiving the data using ajax,but those data are not shown on the page. For example I used bootstrap-table,and it is working, but not in the case of bootstrap-select. Do you have any hints about this topic?

Best regards

Answer:1

You need to initialize selectpicker after you get result from Ajax call successfully:

$(document).ready(function () {
    $.ajax({
        type: "GET",
        url: 'selectPHP.php',
        data: {},
        success: function (response) {
            alert(response);
            $('#players1').html(response);
            console.log((response));
            $('.selectpicker').selectpicker({
                style: 'btn-primary',
                size: 2
            });
        },
        error: function () {
            $('#players1').html('There was an error!');
        }
    });
});
Answer:2

I am having Collapse Panel in Bootstrap which is opening on a click on the title of the tab. I am trying to figure out to open using the hover of the mouse on the total width of the tab but I am not ...

I am having Collapse Panel in Bootstrap which is opening on a click on the title of the tab. I am trying to figure out to open using the hover of the mouse on the total width of the tab but I am not ...

I'm trying to move the dropdown that DataTables uses to determine how many rows to display. One way I thought of was to use jQuery's prependTo()/appendTo() functions, but it feels a bit too hack-y. I ...

I'm trying to move the dropdown that DataTables uses to determine how many rows to display. One way I thought of was to use jQuery's prependTo()/appendTo() functions, but it feels a bit too hack-y. I ...

  1. datatables move pagination to left
  2. datatables move pagination to top
  3. datatables move pagination to center
  4. datatables move pagination
  5. datatables move pagination to bottom
  6. datatables move pagination to right
  7. datatables move pagination outside table
  8. datatables move paging

I'm very new to Javascript and pretty new to CSS/HTML as well. I'm trying to create a vertical menu with CSS and javascript. The sublevels should appear on the right of the menu when someone click on ...

I'm very new to Javascript and pretty new to CSS/HTML as well. I'm trying to create a vertical menu with CSS and javascript. The sublevels should appear on the right of the menu when someone click on ...

I am using the Gantt Chart provided by Google. I have added a listener to when I click on any of the elements or entity in attempt to return the row/columns data, but it is returning empty, ...

I am using the Gantt Chart provided by Google. I have added a listener to when I click on any of the elements or entity in attempt to return the row/columns data, but it is returning empty, ...

  1. google gantt chart template
  2. google gantt chart maker
  3. google gantt chart app
  4. google gantt chart free
  5. google gantt chart dependencies
  6. google gantt chart custom tooltip
  7. google gantt chart react
  8. google gantt chart options
  9. google gantt chart angular
  10. google gantt chart javascript
  11. google gantt chart generator
  12. google gantt chart examples
  13. google gantt chart bar color
  14. google gantt chart scroll bar
  15. google gantt chart jsfiddle
  16. google gantt chart date format
  17. google gantt chart drag and drop