javascript - Why are JQuery autocomplete results not showing in browser? -


i have working fiddle, autocomplete not display in browser. fiddle can seen here: working fiddle

in html, have 1 input element testing purposes:

<head> <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" /> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" /> <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script> <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>  <script type ="text/javascript" src="myscripts.js"></script> <script type ="text/javascript" src="jquery.csv-0.71.js"></script> <script type ="text/javascript" src="csvtoarray.v2.1.js"></script> </head>   <body> <div data-role="page" id="main">      <div data-role="header">         <h1>test</h1>     </div>      <div id="contentdiv" data-role="content">            <input type="text" id="food" placeholder="type (first name) here" />             </div>  </div> </body> 

in javascript, initializing json variable reading text file. have tested initialization successful displaying alert of json variable. trying use json variable source in autocomplete. below, have simplified javascript hard coding initialization of json variable in order narrow down problem:

var jsonversion =        [{"description":"mac , cheese", "servingsize":"1 cup", "calories":"500"},  {"description":"slice of pizza", "servingsize":"1 slice", "calories":"400"},  {"description":"oreo cookie", "servingsize":"1 cookie", "calories":"100"},  {"description":"salad", "servingsize":"1 cup", "calories":"50"},  {"description":"apple", "servingsize":"1 apple", "calories":"70"}];  $('#food').autocomplete({                      source: jsonversion,                      select: function (event, ui) {                      selectedobj = ui.item;                      alert("selected object=" + selectedobj.value);                                                     },                      minlength: 1                         }); 

any idea why work in fiddle not in browser? not getting browser errors. nothing being displayed when type in textbox.

edit

perhaps in way populating jsonversion - although when print jsonversion via "alert", looks right. advice on doing wrong here appreciated. here entire javascript file. "data" array of arrays , looping through each of arrays create object, in turn creating array of objects. convert array of objects json using stringify:

$(function ($) {     var jsonversion;     var arrayofobjects;     $.ajax({             type: "get",             url: "test.js",             datatype: "text",             success: function(data) {                                     data = $.csv.toarrays(data);                                     arrayofobjects = new array();                                     for(var i=1; i<data.length; i++)//skipping on header                                     {                                         var foodobject = new object();                                         foodobject.label = data[i][1];                                         foodobject.weight = data[i][2];                                         foodobject.servingsize = data[i][3];                                         foodobject.calories = data[i][4];                                         arrayofobjects.push(foodobject);                                     }                                     jsonversion = json.stringify(arrayofobjects);                                     alert(jsonversion);                                      }             });      $('#food').autocomplete({                             source: jsonversion,                             select: function (event, ui) {                             selectedobj = ui.item;                             alert("selected object=" + selectedobj.value);                                                             },                             minlength: 1                             }); }) 

you have 2 major problems:

  1. you're passing string source option of autocomplete. when this, widget attempts use string url retrieve results from. since string json representation of array built result of ajax call, not going work way expect to. should use arrayofobjects instead.

  2. you're initializing autocomplete widget outside of success callback ajax request. means autocomplete widget initialized empty source. can fix moving initialization success callback.

fixing both of these things should fix issues:

$(function ($) {     $.ajax({         type: "get",         url: "test.js",         datatype: "text",         success: function(data) {             data = $.csv.toarrays(data);             var arrayofobjects = [];             for(var i=1; i<data.length; i++)//skipping on header             {                 var foodobject = new object();                 foodobject.label = data[i][1];                 foodobject.weight = data[i][2];                 foodobject.servingsize = data[i][3];                 foodobject.calories = data[i][4];                 arrayofobjects.push(foodobject);             }              $('#food').autocomplete({                 source: arrayofobjects,                 select: function (event, ui) {                     selectedobj = ui.item;                     alert("selected object=" + selectedobj.value);                 },                 minlength: 1             });                     }     }); }); 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -