Hi everyone, I’m working on the Random Quote Generator project. Everything else has been going smoothly, but I’m really having some trouble with the Twitter share button.
So in my codepen’s JS section, I have a snippet from the WikiquotesAPI made by Nate Tyler, then the Twitter button’s code, and then my own code. (The button code and my code are in a document event listener for when the page loads, if that helps.)
This is how the Twitter button is written in my HTML:
<a class="twitter-share-button" href="https://twitter.com/share" data-text="" data-size="large" id="twitter"></a>
In my JS right now, this is how I’m trying to insert the quote into the Tweet:
insertQuote = qt => {
...
twitter.setAttribute("data-text", qt.quote);
};
But then I test the Twitter share button and the text box just shows a link to the codepen.
I’ve also tried to add the quote to the href itself (while also using the encodeURIComponent() function), just like this:
<a class="twitter-share-button" href="https://twitter.com/intent/tweet?text=" data-text="" data-size="large" id="twitter"></a>
twitter.setAttribute("href", twitter.getAttribute("href") + encodeURIComponent(qt.quote));
But I just got the same exact result. In both cases, I also looked in the console for any errors, but nothing shows up there. I’m at a loss right now, although I’m thinking that something about the other code (or where I put the document’s event listener and/or the others) is causing the issue. (Like the twitter button doesn’t exist yet when I’m trying to put the quote into its data-text attribute?)
(Also, I need to find a way to make the API pick from random titles - right now, it’s only picking things from Thomas Jefferson because I just set it that way temporarily. :P)
My project:
https://codepen.io/KelbyBaca/pen/BrwZgP