Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get selected value from jqueryui autocomplete combobox

I'm trying to get selected value from jqueryui autocomplete combobox.

Here is the Demo.

I have added below code to get selected value. but it is not worked.

$( "#combobox" ).autocomplete({
   select: function(event, ui) { 
       alert($(ui).val());
   }
});

How can I solve this?

like image 220
Bishan Avatar asked Aug 03 '13 08:08

Bishan


2 Answers

Here is the working fiddle for the solution to your problem: Working Fiddle

 $( "#combobox" ).combobox({
      select: function( event, ui ) {
      alert(ui.item.value);
      }
      });

Just access the value variable of item in ui, you will get the required value of selected option.Just a small change is required in your code and its fixed.

like image 131
Aditya Avatar answered Sep 22 '22 20:09

Aditya


First of all in a "normal" autocomplete you would get the value of the selected item by doing ui.item.value. In your case this doesn't work. Perhaps because the select function gets overridden.

Instead alert the value in your autoCompleteSelect function.

this._on(this.input, {
    autocompleteselect: function (event, ui) {
        /* THIS IS NEW */
        alert(ui.item.value);
        ui.item.option.selected = true;
        this._trigger("select", event, {
            item: ui.item.option
        });
}

If you are curious what other options you could get in this function just do a console.log(ui) in it and open your console to see other options. All in all you don't even need to call the autocomplete widget anymore.

Updated fiddle

like image 43
SirDerpington Avatar answered Sep 22 '22 20:09

SirDerpington