Filtering a select list via input box & jquery

Posted by zSysop on Stack Overflow See other posts from Stack Overflow or by zSysop
Published on 2010-04-23T16:45:36Z Indexed on 2010/04/23 17:23 UTC
Read the original article Hit count: 221

Filed under:
|
|

Hi all,

I was wondering if i could get some help with filtering a select list using an input box via jquery.

Here's what my js looks like, but it doesnt seem to work. I'm guessing this is because options within a select list are not hide-able.

    <script type="text/javascript">
    $(document).ready(function() {
        $("#inputFilter").change(function() {
            var filter = $(this).val();

            $("#selectList option").each(function() {
                var match = $(this).text().search(new RegExp(filter, "i"));
                if (match > 0) {
                    $(this).show(); // Does not work
                }
                else
                    $(this).hide();
            });
        });
    });
</script>

and here's my html

<input id="inputFilter" />
<select id="selectList">
    <option value="1111" text="1111 - London" />
    <option value="1112" text="1111 - Paris" />
</select>

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about selectlist