lov2code
lov2code

Reputation: 394

Jquery autocomplete not updating the input value with selection

I am trying to create an autocomplete field with jquerys famous autocomplete and by creating an array from mysql database. It works perfectly but when a selection is made, the input field value is not updated, therefore I am not able to pass the value from the form. Can someone help me here?

JQUERY:

<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/themes/base/jquery-ui.css " type="text/css" media="all" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js " type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js " type="text/javascript"></script>
<script>
$(document).ready(function() {
  $("#keywords").autocomplete({
    source: keywordList
  });
});

</script>
<?php echo keywordArray(); ?>

PHP: (to create array list for autocomplete)

<?php
include 'admin/dbconn-dest.php';

function keywordArray()
{
  $rsKeywords = mysql_query("SELECT Destination FROM Destinations WHERE Country = 'Mexico'");

  $output = '<script>'."\n";
  $output .= 'var keywordList = [';

  while($row_rsKeywords = mysql_fetch_assoc($rsKeywords))
  {
    $output .= '"'.$row_rsKeywords['Destination'].'",';
  }

  $output = substr($output,0,-1); //Get rid of the trailing comma
  $output .= '];'."\n";
  $output .= '</script>';
  return $output;
}
?>

HTML:

<input id="keywords" name="keywords" type="text" autocomplete="off" size="40" >

Any help would be greatly appreciated!!!

Upvotes: 2

Views: 5594

Answers (2)

Shyju
Shyju

Reputation: 218852

Use the select event to set the value of the input box

$("#keywords").autocomplete({
    source: keywordList,
    select: function (event, ui) {
                         $("#keywords").val(ui.item.value);                       
                     }  
  });

Upvotes: 4

Bagira
Bagira

Reputation: 2243

This is a known issue. Try using Select First plugin. This should do the job for you (At least it worked perfectly fine in my case)

Hope that helps.

Upvotes: 1

Related Questions