jquery livesearch(filter) keeping the selected items

The below code works perfectly for a livesearch on a Html select. But what if I have a multiple select list and I want to be able to select some items, then search for a keyword but not to lose previously selected items.

Any ideas?

<script type="text/javascript">
    $(function() {
        var opts = $('#optlist option').map(function(){
            return [[this.value, $(this).text()]];
        });

        $('#someinput').keyup(function(){
            var rxp = new RegExp($('#someinput').val(), 'i');
            var optlist = $('#optlist').empty();
            opts.each(function(){
                if (rxp.test(this[1])) {
                    optlist.append($('<option/>').attr('value', this[0]).text(this[1]));
                }
            });
        });
    });
</script>

<input id="someinput"><br>

<select id="optlist" multiple="multiple">
    <option value="1">foo</option>
    <option value="2">bar</option>
    <option value="3">foobar</option>
    <option value="4">foobarbaz</option>
</select> 

Upvotes: 0

Views: 800

Answers (1)

I just found a pretty good alternative here

Upvotes: 1

Related Questions