0

I have a select which I am trying to use with bootstrap 3 by using the selectpicker class. I want to be able to get the data from the option the user selects so I'm using the jquery onchange for my selectpicker. I'm pretty confident with finding the selected value and what not from a regular select but when the html gets rendered for the bootstrap selectpicker, it's done differently. My original select gets a display: none; style added to it and then it generates a ssecond div containing all my option items. enter image description here

Here is my select in my template:

        <select id="buildings" class="selectpicker" data-width="80%" data-live-search="true" style="border-radius:0px;" >
            {% for category in building_categories %}
                <optgroup label="{{ category.asBuildingCategory }}">
                    {% for building in buildings %}
                        <option data-content="<span data-latitude='{{building.building.decLatitude}}' data-longitude='{{building.building.decLongitude}}' data-build-id='{{building.building.ixBuilding}}'>{{building.building.asBuilding}}</span>"></option>
                    {% endfor %}
                </optgroup>
            {% endfor %}
        </select>

I have to add a data-content since bootstrap selectpicker won't have my custom data elements after it's rendered its own way.

Now I've seen someone else has done pretty much what I'm doing except mine just won't work. Bootstrap-select - how to fire event on change. The value returned is simply "" because I'm assuming it has something to do with my select getting a display: none; added

Community
  • 1
  • 1
john
  • 3,949
  • 7
  • 34
  • 56

1 Answers1

0

Changing this:

$('.dropdown-menu.inner.selectpicker li').on('click', function () {
});

to this:

$(document.body).on('click','.dropdown-menu.inner.selectpicker li', function () {
});

apparently jquery cannot bind to the class since at binding the classes are slightly different. So instead I'm using the body then checking if the event is being generated by my li. Will give to owner of the answer as soon as I can find his post

john
  • 3,949
  • 7
  • 34
  • 56