Random Quote Generator initialization

I’m having trouble getting a random quote to pull when the page loads. I’m able to get a quote when the button is clicked, but not when the page loads. I’m not sure what I’m doing wrong here… Can anyone take a look?

Thats because only when a user clicks the Next Quote button a random quote is selected and displayed but at the beginning i.e, when the page loads the next quote button is unused so the javascript hasnt even started functioning .

To solve the problem you could add a quote at the place which reads quote so that you have a quote to display even if the user hasnt pressed the new quote button.

So, I want it to display a random quote when the page loads. I created a paragraph with id “quotePar” and then tried setting the paragraph innerHTML with the getRandomQuote() function. I’m not sure why this doesn’t work. Can you explain why? Is there something else I need to run, or perhaps I’m completely missing something?

I moved document.getElementById("quotePar").innerHTML = getRandomQuote(); from between the script tags in the HTML file to the top of the JS file and the quote loaded.

Thanks :slight_smile: That totally worked. But I’m not sure why… Probably something to do with the timing of how pages load. Now that it works, I can look into why.