anonicode anonicode - 3 months ago 10
Javascript Question

filter a select box with jquery

i tryed to made text input that filtering selectbox.
im trying to fix it alot of time but it stil not working well.
mybe becuse it is in hebrew?

html:



<option value="628077"> new york</option>
<option value="244228">רמת גן<option>
<option value="641332"> דימונה</option>
<option value="240812"> הזורעים</option>
.
.
.
.
<option value="640375"> עתניאל</option>
<option value="344580"> חיפה</option>
<option value="440560"> בית רבן</option>
<option value="520098"> הרצליה</option>
</select>


jquery:

<script type="text/javascript" >
$('#city').keyup(function () {

var valthis = $(this).val();
var num = 0;
$('select#DropDownList1>option').each(function () {
var text = $(this).text();
if(text.indexOf(valthis) != -1)
{ $(this).show();}
else{ $(this).hide();}

});

});

</script>


i need your help! thanks!
and sory for my english.

Answer
$('#city').keyup(function () {
    var valthis = $(this).val().toLowerCase();

    $('select#DropDownList1>option').each(function () {
        var text = $(this).text().toLowerCase();
        if (text.indexOf(valthis) !== -1) {
            $(this).show(); $(this).prop('selected',true);
        }
        else {
            $(this).hide();
        }
    });
});

DEMO Hope it helps.