Search code examples
javascriptjquerytwitter-bootstrapbootstrap-select

bootstrap-select javascript instantiation does not create object


It seems that if I create the select and options fields via Javascript / jQuery, it will not create an instance of object selectpicker for the newly created object. In the documentation i cannot find how to force an instanciation.

This is what my code looks like:

$(document).ready(function() {
    var content = $('<select class="selectpicker">'
    +'<option>STRING</option>'
    +'</select>');
    content.find('.selectpicker').selectpicker();
});

This is the error I get from the browser:

TypeError: $(...).selectpicker is not a function


content.find('.selectpicker').selectpicker();

Includes are all correct, using it embedded in the normal HTML code works.

Someone tumbled already over this problem?


Solution

  • make sure bootstrap-select.min.js is added after jquery but before your code.

    remove the find as content is the select

    var content = $('<select class="selectpicker">'
    +'<option>STRING</option>'
    +'</select>');
    content.selectpicker();