Random Quote Generator - button not triggering

Random Quote Generator - button not triggering
0

#1

Hi,

I’m working on the Random Quote Generator and the button is not triggering a new quote.
I’m keeping it simple on this one and using an array that I’ve built.

I’ve looked around but can’t get it to work - I’m missing something. I’m not grasping the concept.

Please, what am I not understanding?

Thanks for your time.

Regards


#2
<button id = "getQuote" class = "button" onclick="getElementById("quote").innerHTML = getRandomQuote()">

On the onclick, you have double quotes inside double quotes which is closing the attribute prematurely and thus causing errors. Replace "quote" with 'quote' for the onclick to fire properly.

Furthermore the onclick should probably just be onclick=getRandomQuote(), if you’re looking to fire that on click.

Good luck!


#3

Looks like you figured part of your problem. You currently have two click event handlers for when the button is clicked. One is in your html (see below) and the other is an addEventListener. Just stick with the addEventListener to separate the html from the JavaScript.

The click event is working, but the following code only runs one time:

var theQuoteAndAuthor = quotesArray[Math.floor(Math.random() * 25)]
var theQuote = theQuoteAndAuthor[0];
var theAuthor = theQuoteAndAuthor[1];

Move this inside your getRandomQuote function, so it will run each time the button is clicked. Also, the proper syntax for the addEventListener should be:

document.addEventListener("click", getRandomQuote); // without the () on the end.  The function getRandomQuote will run, because it is a callback function that gets called after the click event.

#4

Thanks!

D’uh! of course those lines should be in the function. that was dumb slip-up. that’s fixed that.
Thanks again.

Regards