Note that there are some explanatory texts on larger screens.

plurals
  1. POadding variable into an array in a function
    text
    copied!<p>I have a problem with putting a variable into my array. Here is my code:</p> <pre><code>var info = new Array(); google.load("feeds", "1"); function initialize() { var feed = new google.feeds.Feed("http://www.ntvmsnbc.com/id/24927681/device/rss/rss.xml"); feed.setNumEntries(6); feed.load(function(result) { if (!result.error) { var container = document.getElementById("feed"); var html = ''; for (var i = 0; i &lt; result.feed.entries.length; i++) { var entry = result.feed.entries[i]; var a = " " ; a += entry.title; info[i] = a html += '&lt;p&gt;' + entry.publishedDate + '&amp;nbsp' + entry.title; } container.innerHTML = html; } alert(info[0]); }); //alert(info[0]); } //alert(info[0]); google.setOnLoadCallback(initialize); </code></pre> <p>You see an array called info. I'm trying to add entry.title into it. You can see some places are commented out. There my info[0] is empty. I can display my result only in function(result), except that it doesn't display anything like I never put anything inside my array. I didn't understand why. </p> <p>Updated:</p> <pre><code> var info = new Array(); google.load("feeds", "1"); function initialize(cb) { var feed = new google.feeds.Feed("http://www.ntvmsnbc.com/id/24927681/device/rss/rss.xml"); feed.setNumEntries(6); feed.load(function(result) { if (!result.error) { var container = document.getElementById("feed"); var html = ''; for (var i = 0; i &lt; result.feed.entries.length; i++) { var entry = result.feed.entries[i]; html += '&lt;p&gt;' + entry.publishedDate + '&amp;nbsp' + entry.title; cb(entry.title); } container.innerHTML = html; } }); } google.setOnLoadCallback(function(){ initizalize(processInfo);} ); function processInfo(information){ info[info.length] = information; alert(info[info.length]); } </code></pre> <p>Last Version</p> <pre><code> var info = new Array(); google.load("feeds", "1"); function initialize(cb) { var feed = new google.feeds.Feed("http://www.ntvmsnbc.com/id/24927681/device/rss/rss.xml"); feed.setNumEntries(6); feed.load(function(result) { if (!result.error) { var container = document.getElementById("feed"); var html = ''; for (var i = 0; i &lt; result.feed.entries.length; i++) { var entry = result.feed.entries[i]; var a= " "; a += entry.title; info[i] = a; html += '&lt;p&gt;' + entry.publishedDate + '&amp;nbsp' + entry.title; } container.innerHTML = html; } cb(info); }); } google.setOnLoadCallback(function(){ initizalize(processInfo);} ); function processInfo(info){ alert(info[0]); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload