Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change textbox value on select event with autocomplete (jQuery UI)

Why won't #input-myBox clear when I select an item? It seems autocomplete is preventing my .val('') to work so how can I workaround this?

$("#input-myBox").autocomplete({
    source: response,
    minLength: 1,
    select: function(event, ui) {
                var selectedObj = ui.item;
                $("#input-myBox").appendTo(".foo");
                $("#input-myBox").val('');  
    }
});
like image 615
el_pup_le Avatar asked Nov 27 '11 11:11

el_pup_le


3 Answers

event.preventDefault() stops autocomplete setting the field.

select: function (event, ui) {
  event.preventDefault();
  var selectedObj = ui.item;
  $("#input-myBox").appendTo(".foo");
  $("#input-myBox").val('');  
}
like image 200
el_pup_le Avatar answered Nov 13 '22 19:11

el_pup_le


Also, you can use 'return false;' to stop autocomplete setting the field.

select: function (event, ui) { 
    var selectedObj = ui.item;        
    $("#input-myBox").appendTo(".foo");
    $("#input-myBox").val('');  
    return false;
}
like image 9
kd12 Avatar answered Nov 13 '22 19:11

kd12


If you just want to substitute the selected value for something else:

select: function( event, ui ) {
  ui.item.value = substituteWord(ui.item.value);
}
like image 4
HerrimanCoder Avatar answered Nov 13 '22 21:11

HerrimanCoder