Get inputted text value of bootstrap selectpicker live search

Viewed 3986

Currently building a website in meteor.js, and I can't seem to figure out how to get the inputted text from my bootstrap selectpicker -- the live search text box doesn't have its own id. My HTML looks like this:

    <div class="filter-component">
        <h5 class="text-center text-danger"> Name: </h5>
         <select ng-model="name" class="nameSelect" data-live-search="true" data-style="btn-primary" ng-options="x for x in names"> <option value="">Nothing selected</option>
          </select>
    </div>
2 Answers

You need to write a custom code to get the value of text which write in the input box. See the example bellow:

Selectpicker HTML:

<div class="filter-component">
     <h5 class="text-center text-danger"> Name: </h5>
     <select ng-model="name" class="nameSelect" data-live-search="true" data-style="btn-primary" ng-options="x for x in names"> <option value="">Nothing selected</option>
      </select>
</div>

Selectpicker jQuery:

$(document).on('keyup', '.filter-component .bs-searchbox input', function (e) {
    var searchData = e.target.value;
    console.log(searchData);

    //You can write the ajax code here
});

Am posting this hope to help some one based on the code founded here https://github.com/codejq/comboxme/tree/master

add the following css and js in your page after bootstrap-select

<style>
     .comboxme .dropdown-menu .no-results {
        display: none !important;
    }
    .comboxme .dropdown-menu {
        top: unset !important;
        bottom: unset !important;
        padding: 0 !important;
        margin: 0 !important;
        border: 0 !important;
    }
    .comboxme .bs-searchbox {
        padding: 0 !important;
        margin: 0 !important;
        border: 0 !important;
    }
    .comboxme .btn {
        background-color: white;
        color: blue;
    }
    .comboxme .bs-caret {
        color: white;
    }
</style>


<script>
        $(function () {
            $(".bs-searchbox input").blur(function () {
                var numberOfActiveItems = $(this).closest(".dropdown-menu").find("li.active").length;
                if (numberOfActiveItems > 0) return;
                console.log(numberOfActiveItems);
                var selectBoxName = $(this).closest(".comboxme").find("select")[0].name;
                $("#" + selectBoxName + " option[value='-1']").remove();
                $("#" + selectBoxName)
                    .append('<option value="-1" selected>' + $(this).val() + '</option>')
                    .selectpicker('refresh');
                $("#" + selectBoxName + "_add").remove();
                $(this).closest("form").append('<input type="hidden" id="' + selectBoxName + '_add" name="' + selectBoxName + '_add" value="' + $(this).val() + '" />');
            });
        });
</script>

in your html add class comboxme to the select like

<select data-width="280px" data-live-search="true" class="comboxme form-control" data-val="true" 
data-val-required="The Status field is required." id="Status" name="Status">
<option value="1">Colorado</option>
<option value="2">Connecticut</option>
<option value="3">Delaware</option>
<option value="4">Florida</option>
<option value="6">Georgia</option>
<option value="7">Kentucky</option>
</select>
Related