Random Quote challenge. Help on Social Share buttons

Random Quote challenge. Help on Social Share buttons
0.0 0

#1

Hello,
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()?


#2

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):


#3

Thank you for your support but I am still lost.


#4

Can you explain why?


#5

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?


#6

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


#7

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.

eg.

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

Instead of

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


#8

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.