# Help with tweet out quote button

**URL:** <https://forum.freecodecamp.org/t/help-with-tweet-out-quote-button/123945>\
**Category:** General\
**Created:** [June 6, 2017, 1:47am UTC](https://forum.freecodecamp.org/t/help-with-tweet-out-quote-button/123945 "2017-06-06T01:47:39Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![kschmatt](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kschmatt/32/58081_2.png) [@kschmatt](https://forum.freecodecamp.org/u/kschmatt)\
**Post date:** [June 6, 2017, 1:47am UTC](https://forum.freecodecamp.org/t/help-with-tweet-out-quote-button/123945/1 "2017-06-06T01:47:40Z")

</div>

I’m having trouble making the tweet button actually tweet out the quote that’s currently on the screen. I really just don’t understand it all and I can’t get it to work and it’s so frustrating. This is what I have done so far: [https://codepen.io/schmattycake/pen/EXxoRe](https://codepen.io/schmattycake/pen/EXxoRe)

Please halp

---

<div class="post-metadata">

**Author:** ![cperry24](https://avatars.discourse-cdn.com/v4/letter/c/2acd7d/32.png) [@cperry24](https://forum.freecodecamp.org/u/cperry24)\
**Post date:** [June 6, 2017, 2:20am UTC](https://forum.freecodecamp.org/t/help-with-tweet-out-quote-button/123945/2 "2017-06-06T02:20:20Z")

</div>

Hey there!

It looks like you are trying to use jQuery for part of the project but not including it in your project. That is the first thing i would check to get the project fully functional.

Check the console on your function errors, they are causing you problems as well.

Good Luck!

---

<div class="post-metadata">

**Author:** ![liberaltech](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/liberaltech/32/160402_2.png) [@liberaltech](https://forum.freecodecamp.org/u/liberaltech)\
**Post date:** [June 6, 2017, 4:15am UTC](https://forum.freecodecamp.org/t/help-with-tweet-out-quote-button/123945/3 "2017-06-06T04:15:20Z")

</div>

Your going to need `encodeURI()` for the twitter url and two of the following functions:

```auto
.click(function(){

})

```

---

<div class="post-metadata">

**Author:** ![fentablar](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/fentablar/32/41967_2.png) [@fentablar](https://forum.freecodecamp.org/u/fentablar)\
**Post date:** [June 6, 2017, 4:49am UTC](https://forum.freecodecamp.org/t/help-with-tweet-out-quote-button/123945/4 "2017-06-06T04:49:35Z")

</div>

you do need the encodeURI, but you don’t need the .click function, FYI.

Something like this:

```auto
$(this).attr("href", 'https://twitter.com/intent/tweet?text=' + encodeURIComponent(quoteMachine[randomNumber]));

```

---

<div class="post-metadata">

**Author:** ![kschmatt](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kschmatt/32/58081_2.png) [@kschmatt](https://forum.freecodecamp.org/u/kschmatt)\
**Post date:** [June 6, 2017, 6:02pm UTC](https://forum.freecodecamp.org/t/help-with-tweet-out-quote-button/123945/5 "2017-06-06T18:02:48Z")

</div>

> [@fentablar](#):
>
> $(this).attr(“href”, ‘[https://twitter.com/intent/tweet?text=](https://twitter.com/intent/tweet?text=)’ + encodeURIComponent(quoteMachine[randomNumber]));

I added this to my code but it still doesn’t tweet the quote out. I really don’t understand what I’m doing wrong and I’m getting really frustrated with myself.

---

<div class="post-metadata">

**Author:** ![fentablar](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/fentablar/32/41967_2.png) [@fentablar](https://forum.freecodecamp.org/u/fentablar)\
**Post date:** [June 6, 2017, 6:05pm UTC](https://forum.freecodecamp.org/t/help-with-tweet-out-quote-button/123945/6 "2017-06-06T18:05:28Z")

</div>

taking a look, just a sec…

---

<div class="post-metadata">

**Author:** ![kschmatt](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kschmatt/32/58081_2.png) [@kschmatt](https://forum.freecodecamp.org/u/kschmatt)\
**Post date:** [June 6, 2017, 6:06pm UTC](https://forum.freecodecamp.org/t/help-with-tweet-out-quote-button/123945/7 "2017-06-06T18:06:26Z")

</div>

thanks. i tried to put it in the .click(function() {{) as well and it still doesn’t work.

---

<div class="post-metadata">

**Author:** ![fentablar](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/fentablar/32/41967_2.png) [@fentablar](https://forum.freecodecamp.org/u/fentablar)\
**Post date:** [June 6, 2017, 6:27pm UTC](https://forum.freecodecamp.org/t/help-with-tweet-out-quote-button/123945/8 "2017-06-06T18:27:52Z")

</div>

Ok so three things:

First, a quote wasn’t loading to the screen because even tho you had the function defined to determine which quote to use and where on the page to put it, there wasn’t any actual call to the function itself.

Second, you’re using a jQuery (this) to define the href for the twitter link, but (this) has no frame of reference, or at least not the one you want; so instead of $(this) it would be better to use $(".twitter-share-button"), and it would also be better served to place that line into the newQuote() function so that when newQuote() is executed it defines both the quote and the desired twitter link.

Third is that you don’t have jQuery loaded in your CodePen. Not that it’s requisite to have jQuery to do what you’re doing, but you would need to modify your reference to the twitter link to use vanilla JS if you aren’t going to use jQuery.

I did a fork of your codepen, added jQuery and modified the code to make it work:

```auto
 function newQuote() {
   var randomNumber = Math.floor(Math.random() * (quoteMachine.length));
document.getElementById('words').innerHTML = quoteMachine[randomNumber];
    $(".twitter-share-button").attr("href", 'https://twitter.com/intent/tweet?text=' + encodeURIComponent(quoteMachine[randomNumber]));
  }

$(function() {
  newQuote();
});

```

the $(function()… bit is the equivalent of doing a $(document).ready(function()… after doing the above, the page loaded the quote properly and the twitter link included the quote as well.

---

<div class="post-metadata">

**Author:** ![OnomeSotu](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/onomesotu/32/3724_2.png) [@OnomeSotu](https://forum.freecodecamp.org/u/OnomeSotu)\
**Post date:** [June 6, 2017, 6:29pm UTC](https://forum.freecodecamp.org/t/help-with-tweet-out-quote-button/123945/9 "2017-06-06T18:29:15Z")

</div>

Hi, I had this same exact problem with my random quote machine i just completed here: [quote machine](https://onomesotu.github.io/quote-machine/)

It drove me mad! I know how you feel.

Your code is wrong. You used `$(this)` when you have not referred to the object. instead of `$(this)`, use something like the ID `$("#tweet").attr(....................)`.

Now another problem is your tweet button. It has a function called `myFunction()` and I could not find that function in your JS code. you should delete if you are not using it.

And if you are going to use jquery, you need a `$(document).ready()` function which the jquery runs when the page has load.

You have to rearrange your code again.

---

<div class="post-metadata">

**Author:** ![fentablar](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/fentablar/32/41967_2.png) [@fentablar](https://forum.freecodecamp.org/u/fentablar)\
**Post date:** [June 6, 2017, 6:30pm UTC](https://forum.freecodecamp.org/t/help-with-tweet-out-quote-button/123945/10 "2017-06-06T18:30:16Z")

</div>

Also also, you may want to add the twitter js library so you get the little pop-up window instead of going to a new tab/window: just add [https://platform.twitter.com/widgets.js](https://platform.twitter.com/widgets.js) to the JS settings of your codepen

---

<div class="post-metadata">

**Author:** ![kschmatt](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kschmatt/32/58081_2.png) [@kschmatt](https://forum.freecodecamp.org/u/kschmatt)\
**Post date:** [June 6, 2017, 6:36pm UTC](https://forum.freecodecamp.org/t/help-with-tweet-out-quote-button/123945/11 "2017-06-06T18:36:30Z")

</div>

Thank you sooooo much. I really didn’t know how to fix my code to make it work. I see that I had /some/ idea of what I needed to do but now I realize where I went wrong. Thank you so dang much.

---

<div class="post-metadata">

**Author:** ![fentablar](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/fentablar/32/41967_2.png) [@fentablar](https://forum.freecodecamp.org/u/fentablar)\
**Post date:** [June 6, 2017, 6:46pm UTC](https://forum.freecodecamp.org/t/help-with-tweet-out-quote-button/123945/12 "2017-06-06T18:46:18Z")

</div>

no worries – squanchy idea for your page by the way!

---

<div class="post-metadata">

**Author:** ![kschmatt](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kschmatt/32/58081_2.png) [@kschmatt](https://forum.freecodecamp.org/u/kschmatt)\
**Post date:** [June 8, 2017, 1:03am UTC](https://forum.freecodecamp.org/t/help-with-tweet-out-quote-button/123945/13 "2017-06-08T01:03:27Z")

</div>

hahahaha, thank you!

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 9:36am UTC](https://forum.freecodecamp.org/t/help-with-tweet-out-quote-button/123945/14 "2021-01-17T09:36:54Z")

</div>

Closed on January 17, 2021, 9:36am UTC


