Javascript global variable doesn't seem to be incrementing?

Javascript global variable doesn't seem to be incrementing?
0

#1

Hello all, I am making a random quote generator. My problem is with my Tweet intent button. It seems to only pre-fill the tweet box with the first quote in array[] quotes. The variable ‘index’ I’m using is a global variable and is incremented every time a new quote is generated so I don’t understand why it keeps acting like index is still 0. Here is my code pen link: https://codepen.io/david_vu408/pen/MJJrBv


#2

The index variable is updating correctly.
The problem with your tweet button is that you only set its href attribute when you first load the page. You would want to update it also when you renew the quote.

So, you should put this code before the index++:

$("#tweet").attr("href", "https://twitter.com/intent/tweet?text=" + encodeURIComponent('"' + quotes[index] + '" ' + authors[index]));

#3

Ahhh I see! Thank you, that fixed it!