I have an XML file dynamically created from a database where each item has both an id and name. In the following syntax:
<item>
<id>1</id>
<name>FirstName</name>
</item>
... and so on ...
I am trying to use these values for a jQuery autocomplete, where the ID will be submitted via form when the name is selected from the autocomplete.
I've tried the following block of code (executed from a button within a form):
function listItems(){
var dataString = "";
dataString += $("form#getItemsForm").serialize();
$.ajax({ type: 'GET', url: 'myXML.xml', dataType: 'xml', success: function(xml){
var myList = new Array();
$('restaurant', xml).each(function() {
var myId = Number($(this).find('id').text());
var myName = $(this).find('name').text();
myList[myId] = myName;
});
alert(myList);
$('.listItems').autocomplete({source:myList});
}});
}
However, this is not working as the IDs begin at 1 and the array is searching for an index of zero so when I alert myList it popups with ,FirstName,SecondName (instead of FirstName,SecondName -- no leading comma).
I've tried making an array of objects but that's a little more cryptic to me as my alert returns [object Object] for each item.
Any pointers would be helpful. Thanks!