Aadam
Aadam

Reputation: 1541

remove selected option from jquery autocomplete

i am using jquery autocomplete and its working fine, my requirement is that the value which is selected much be removed from the drop down how can i achieve that? js and html are below

js

var classes=[];
            $.ajax({
                    url: "classes.htm",
                    dataType : "json",
                    success: function(data) {
                        var val = data;
                        var arr=val.toString().split(",");
                        for(var i=0;i<arr.length;i++)
                            {
                                classes.push(arr[i]);
                            }

                    }
                    ,
                    error:function(jqXHR, textStatus, errorThrown){
                    alert(textStatus);
                    }
                });

            $( "#cls" ).autocomplete({
                source: classes
            });
            $("#cls").bind("autocompleteselect", function(event, ui) {

            });

html

<input type="text"  name="cls" id="cls"/>

Thanks and Regards

Upvotes: 0

Views: 4521

Answers (1)

Fran Verona
Fran Verona

Reputation: 5476

Just overwrite the source option of your Autocomplete element when one element of your list is selected:

$("#cls").bind("autocompleteselect", function (event, ui) {
   // Remove the element and overwrite the classes var
   classes.splice(classes.indexOf(ui.item.value),1);

   // Re-assign the source
   $(this).autocomplete("option","source",classes);
});

Be careful because when the user select an option, it will be removed automatically.

Upvotes: 3

Related Questions