Random Quote challenge. Help on Social Share buttons

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

I have used .attr() and given href with the attribute of the URL. Is there an error how I use the .attr()?

I used this code for tweeting:

 $('.twitter-share-button').click(function() {
      var tweetQuote = $(".quote").text();
      if (tweetQuote != '') {
        window.open('https://twitter.com/intent/tweet?text="' + tweetQuote + '"', '_blank');

Note text= not ext=

Warning, spoiler! Fixed version (uses jQuery):

1 Like

Thank you for your support but I am still lost.

Can you explain why?

I chaged the .attr() to window.open( url + var)

$(’#tweet’).on(“click”, function() {
var qtweet = $(".random").text();
window.open(‘https://twitter.com/intent/tweet?text=’ + qtweet);

But same is not working.

I think i missed an import point in freecodecamp tutorial?

Sorry, what is not working? If you look at my fork of your code, that works.

Here is the HTML.

<a href =“https://twitter.com/intent/tweet?text= " + tweetLink” class=" fa fa-twitter" data-size=“large” id = “tweet” target = “blank” >Tweet

Here is Javascript.

$(’.fa fa-twitter’).on(“click”, function() {
var tweetLink = $(".getQuoteButton").text();
window.open(‘https://twitter.com/intent/tweet?text= + tweetLink’ );

When I click the Twitter button the code sends a URL of Twitter only and not Quote.



Instead of

https://twitter.com/intent/tweet?text= random quote.

That $(’.fa fa-twitter’).on is jQuery, so you must add the jQuery library to your code for it to work. However, since 99% of your code does not use jQuery, I would just use the plain JavaScript addEventListener method for the click event.

Also, since your button is an anchor tag instead an a button element, you will need to learn about the preventDefault function or when you click on the Tweet link, it will just use the url in the href and ignore your window.open. Another to resolve this is to change the anchor tag to a button element, so you do not have to use the preventDefault function.