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>
The variable
datacontains all of the facts in an array. When the user clicks, the functionseedis randomly selecting one item out of the array.So, to display them all:
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: