codeispoetry
codeispoetry

Reputation: 373

jquery autocomplete with more items in the same fields

I use this function to autocomplete country after enter town

 $(function() {
function log( message ) {
  $( "<div>" ).text( message ).prependTo( "#log" );
  $( "#log" ).scrollTop( 1 );
}

$( "#town2" ).autocomplete({
  source: "<?php echo $absolute_site . "autocomplete/autocompletetown.php" ?>",
  select: function( event, ui ) {
                    var item = ui.item; // probably modification goes here...
                if(item) {
                    $("#country2").val(item.country);                       
                }

  }
})
    .autocomplete( "instance" )._renderItem = function( ul, item ) {
  return $( "<li>" )
    .append( "<a>" +item.value + "," +item.state + "," + item.country + "</a>" )
    .appendTo( ul );
};

});

GOAL How can I have the item.value and the item.state in the field #town2, so that after selecting the town in the field #town2 I will have something like

Miami is in Florida (where Miami is item.value and Florida is item.state). I did search everywhere, but I can find only multiple values (and not multiple items...) Thanks!!

Upvotes: 0

Views: 74

Answers (1)

charlietfl
charlietfl

Reputation: 171669

Should be able to simply set the value the same way you do for country. Also return false to prevent plugin itself setting the value

 select: function( event, ui ) {
        var item = ui.item; 
        if(item) {
              $("#country2").val(item.country); 
              $(this).val(item.value +' is in ' + item.state);   
              return false;                   
        }    
  }

Upvotes: 1

Related Questions