I am calling an javascript variable inside autocomplete function and and making url in ajax from textbox value but it is showing undefined.
function fetchRecords()
{
var searchStr=$("#tags").val();
var finalLink=urlformjavascriptvariable;
$.ajax({
url:finalLink,
type: 'GET',
dataType :'jsonp',
async: false,
crossDomain:true,
success: function(data) {
var resp = data.response;
var availableTags = "[";
for (i = 0; i <= 10; i++) {
if(resp.messages.message[i].subject.$ != undefined)
var postSub = resp.messages.message[i].subject.$;
if (i < 10) {
availableTags +="\"" +postSub + "\",";
}
else {
availableTags +="\""+ postSub+"\"";
}
}
availableTags += "]";
availableTags=eval(availableTags);
$("#tags").autocomplete({
source: availableTags,
});
}
});
}
</script>
<div class="ui-widget">
<input id="tags" class="input_width mgtop20" autocomplete="on" onKeyup="fetchRecords();" type="text" name="post_search" value="" placeholder="Search the Community for answers" /></div>
It shows variables undefined
for (i = 0; i <= 10; i++) {
This is the offending line of code. The ajax call is going past the number of items in your list.
Switching the same statement to
for (i = 0; i < resp.messages.message.length; i++) {
will resolve the issue.
User contributions licensed under CC BY-SA 3.0