I want to limit the results to max 10 but my code below doesn’t work. I get Uncaught SyntaxError: Unexpected identifier error on line var results = 'search-db.php';
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$('#textbox_postcode').autocomplete(
{
var results = 'search-db.php';
source: response(results.slice(0, 10));,
minLength: 3
});
});
</script>
</head>
<body>
<input type="text" id="textbox_postcode" value="" />
</body>
</html>
As I already said in my comment, this is not valid JavaScript.
The documentation describes the various ways how to retrieve the data.
Since you want to limit the number of items shown, you have two possibilites:
Let the server return only 10 items. This would be the most sensible solution since you avoid transferring data you won’t use anyway.
Use a callback as
source, make the Ajax request and prepare the data accordingly.Here is an example for the second solution: