Click button issue - Works only once then I need to refresh the page for it work again - solved Now stuck with social share buttons

Click button issue - Works only once then I need to refresh the page for it work again - solved Now stuck with social share buttons
0.0 0

#1

Hello,

I am working on random quote machine challenge. I created the program in code pen https://codepen.io/vickkymevawala/pen/aqBgaJ. Once the user clicks on a button " Click to pop a Quote" a random quote appears.

The problem I face is click button. It only works once. Need to refresh the page for the click to be functional again. How do I make the button work multiple times and on every click pop a random quote?


#2

The problem you are having is because the random number you assign to random is only every read once—you can verify this by logging random inside myFunction(). You just need to change your code such that a new random integer is generated every time myFunction() is called, and I believe you already know how to do that. :slight_smile:

On another note, it is worth noting that are a few other ways you can call myFunction() using just JavaScript instead of having that in HTML (the latter is probably less maintainable). For example:

var getQuoteButton = document.getElementById('getQuoteButton');

getQuoteButton.onclick = myFunction;

Or this:

var getQuoteButton = document.getElementById('getQuoteButton');

getQuoteButton.addEventListener('click', myFunction);

Good luck with the rest of the quote machine. :smile:


#3

Thanks Honman!!!

I was struggling with this since twenty days and almost gave up as I could not find any material online to my solution.

I still need to understand the additional advice you gave but the main problem is resolved.

I am yet to figure out the share of the quote to the social site.

Thanks a ton.


#4

Hello Honmanyau,

Post your help, My earlier problem is solved but stuck with social share buttons.

Both my LinkedIn and Tweeter but don’t work. Tweet share the URL of my pen while lindedin generate a bank post.