I'm trying to write a jQuery UI autocomplete by reading an external file (i.e. data.json). The code works fine when I try to read it from an array but if I try to read it from an external file, it doesn't work anymore!?! Not sure why!!
Here's my code:
http://plnkr.co/edit/LPqBGyocpswPrEzjb1Nq?p=preview
OR
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Autocomplete functionality</title>
<link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script>
$(function() {
$( "#autocomplete1" ).autocomplete(
{
source:"data.json",
select: function( event, ui ) {
$( "#autocomplete1" ).val( ui.item.code + " - " + ui.item.label );
return false;
}
}).data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.code + " - " + item.label + "</a>" )
.appendTo( ul );
};
});
</script>
</head>
<body>
<input type="text" id="autocomplete1" size=40/>
</body>
</html>
/********************************************************/
Here's the data.json file:
{
{
"code":"YOO",
"label":"Oshawa",
"country":"Canada",
},
{
"code":"YOW",
"label":"Ottawa Macdonald-Cartier International",
"city":"Ottawa",
},
{
"code":"YOH",
"label":"Oxford House",
"city":"Oxford House",
}
}
tks