I am trying to use javascript and jquery to build an HTML table based on information returned from a database. I have the JSON and the database hit working properly, but I am unable to show the data on my page.
This is code to display the data
var temp;
var init = function() {
$(_getMarketInfo()).appendTo(document.body);
// Used for logging
temp = $(_getMarketInfo());
console.log(temp);
};
And then to get and process the data
function _getMarketInfo() {
$.ajax({
url:'scripts/db_getMarketInfo.cgi',
dataType:'json',
success:function(data, testStatus) {
var html = '';
html +='<div class="marketInfoHolder">';
html +='<table class="tbl" border=1>';
html +=' <tr><th>Market Information</th></tr>';
html +=' <tr><td>Market</td></tr>';
for (var i=0;i< data.marketInfo.length; i++){
html += '<tr><td>' + data.marketInfo[i].market_name + '</td></tr>';
}
html +='</table>';
html +='</div>';
//used for logging
console.log(html);
return html;
},
error:function(data, textStatus) {
alert('There was an error getting market information.');
}
});
};
According to the console logs, the html variable does have proper html code for a table, but then when I look at temp variable, it is logged as []. It appears that, for some reason, the _getMarketInfo() isn't properly returning html to temp.