I have realised that for my autocomplete I don’t really need to work with an ajax call(I have seen many examples that worked with ajax call), I can simply work with the following:
$(document).ready(function () {
var myData = jQuery.parseJSON(getProjects());
$("#inputSearchProject.ClientID").autocomplete(myData);
});
Where, getProjects() is a small script and TheProjects is a property that it is initialized with the suggestion JSON string for my autocomplete.
<script type="text/javascript">
function getProjects() {
return '<%= this.TheProjects %>';
}
</script>
When debugging I have noticed that myData receives the values, but I still get an runtime error : “Microsoft JScript runtime error: Object doesn’t support property or method ‘autocomplete'” – even though I have searched, I haven’t found the answer to this.
EDIT: I have solved the error (it was a silly one:) novice kind of mistake)
I have changed my code into:
$(document).ready(function () {
var myData = jQuery.parseJSON(getProjects());
$("[id$='txtSearchProject']").keypress(function ()
{
$("[id$='txtSearchProject']").autocomplete(myData);
})
});
because I want autocomplete to be started only when it detects I have written something in the input control. Do you have any suggestions why it doesn’t work?
Thank you all for your suggestions. I have solved my problem, but only with the tips I have received from you. I am going to put here my small piece of code, maybe someone with a similar question will find it useful in the future:
$(document).ready(function () {
var myData = jQuery.parseJSON(getProjects());
$("[id$='txtSearchProject']").keypress(function ()
{
$("[id$='txtSearchProject']").autocomplete({ source: myData});
})
});
//I have added instead of .autocomplete(my data) —>.autocomplete({ source: myData})
You are missing the jQuery UI script file.
Get it here:
http://jqueryui.com/download