JavaScript help needed (YQL and JavaScript)

I have this "adapted" javascript code to display funfacts about anything (in this case about movies and/or History) However, it is built to display only one fact at a time, the user needs to press a button to display another funfact.

My question is, is there a way to display all the facts at once and not just one at a time?

function funfacts(o){
  var facts = document.getElementById('funfacts');
  facts.className = 'js';
  if(facts){
    var data = o.query.results.p;
    var link = facts.getElementsByTagName('a')[0];
    link.innerHTML = '(see all facts)';
    var out = document.createElement('p');
    out.className = 'fact';
    facts.insertBefore(out,link.parentNode);
    function seed(){
      var ran = parseInt(Math.random()*data.length);
      out.innerHTML = data[ran];
    }
    var b = document.createElement('button');
    b.innerHTML = 'get another fact';
    b.onclick = seed;
    link.parentNode.insertBefore(b,link);
    seed();
  }
}

  <script src="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D'http%3A%2F%2Fwww.tealdragon.net%2Fhumor%2Ffacts%2Ffacts.htm'%20and%20xpath%3D'%2F%2Fli%2Fp'&format=json&diagnostics=true&callback=funfacts"></script>  

Answers


The variable data contains all of the facts in an array. When the user clicks, the function seed is randomly selecting one item out of the array.

So, to display them all:

out.innerHTML = data.join("<br />");

This joins the facts into a single string, separated by newlines.

Edited to add:

Here's an example of a complete, very simple web page that displays all of the items:

<html>
<body>
<div id="funfacts">
</div>
<script>
function funfacts(o){
  var facts = document.getElementById('funfacts');
  if(facts){
    var data = o.query.results.p;
    var out = document.createElement('p');
    out.className = 'fact';
    facts.appendChild(out);
    out.innerHTML = data.join("<br />");
  }
}
</script>
<script src="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D'http%3A%2F%2Fwww.tealdragon.net%2Fhumor%2Ffacts%2Ffacts.htm'%20and%20xpath%3D'%2F%2Fli%2Fp'&format=json&diagnostics=true&callback=funfacts"></script>

</body>
</html>

Need Your Help

Call to member function on a non-object with instance variable

php oop object count instance

I have the below code which, when run, returns this error:

jsch-->Not able to connect from unix jump server to another unix servers using java

java unix jsch

I am not able to connect secured server one unix server to another unix server.From putty by ssh i am able to connect easily but i am geeting below error when connet from jsch.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.