Long title, but I wanted to get across what I was trying to do. If I had and XML file in a document library (you could mail enable the doc lib, and send XML from a query in SQL server using the “FOR XML” statement!), could I use jquery to add Autocomplete values to an input field in a newform.aspx? Ended up being not that difficult.
- Once the ajax call sucessfully retrievs the xml file, the parseXml routine is called.
- Seems there is an issue that IE will not think the file is XML, but rather as txt, so there some quick code to load the file as XML
- Then I just look in the file and grab the values I want, and append them to an array
- Then just set the array to be used as the autocomplete source
010203040506070809101112131415161718192021222324252627282930313233<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "GET",
url: "http://URL/To/xmlfile.xml",
dataType: ($.browser.msie) ? "text" : "xml",
success: parseXml
});
});
function parseXml(data)
{
var xml;
if (typeof data == "string") {
xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
xml.loadXML(data);
} else {
xml = data;
};
var results = [];
$(xml).find("ID").each(function(){
var ClientName = $.trim($(this).find('Name').text());
var ClientCode = $.trim($(this).find('ZipCode').text());
results[results.length] = ClientName + "(" + ClientCode + ")"
});
$('input[title=Title]').autocomplete({
source: results,
delay:10,
minLength: 3
});
};
</script>
Comments are closed.