Use a function to set a variable with Javascript?
<p>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.</p>

<p>This is code to display the data</p>

<pre><code>var temp;

var init = function() {
    $(_getMarketInfo()).appendTo(document.body);

    // Used for logging
    temp = $(_getMarketInfo());
    console.log(temp);
};
</code></pre>

<p>And then to get and process the data</p>

<pre><code>function _getMarketInfo() {
    $.ajax({
        url:'scripts/db_getMarketInfo.cgi',
        dataType:'json',
        success:function(data, testStatus) {
            var html = '';
            html +='&lt;div class="marketInfoHolder"&gt;';
            html +='&lt;table class="tbl" border=1&gt;';
            html +='    &lt;tr&gt;&lt;th&gt;Market Information&lt;/th&gt;&lt;/tr&gt;';
            html +='    &lt;tr&gt;&lt;td&gt;Market&lt;/td&gt;&lt;/tr&gt;';

            for (var i=0;i&lt; data.marketInfo.length; i++){
                html += '&lt;tr&gt;&lt;td&gt;' + data.marketInfo[i].market_name + '&lt;/td&gt;&lt;/tr&gt;';
            }

            html +='&lt;/table&gt;';
            html +='&lt;/div&gt;';

            //used for logging
            console.log(html);

            return html;
        },
        error:function(data, textStatus) {
            alert('There was an error getting market information.');
        }
    });
 };
</code></pre>

<p>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.</p>
