Need help with Random Quote Generator Tweet

I’m having some difficulty with inserting the current quote in the tweet, when I call the function the two strings give back a value of undefined. Can anyone help?
Here is the link to the pen:

You need an open bracket on the else; before the newQuote(); at line 16

1 Like

Twitter can’t convert your tweet to a url just by adding it to the url. it has to be encoded
Do this:
var tweet = ‘’ + encodeURIComponent(tweet_quote + ’ \n’ + tweet_author)

1 Like"" +

This worked for me… @LorTos did correctly by assigning and calling the random number. There was just a bracket that didn’t match - those pesky things want to be opened and closed.

I thought you could omit the brackets in the ‘if else statement’ if the statement is only one line

It still becomes undefined! I’m starting to wonder if there is a problem with my selectors ‘#quote p’ and ‘#author p’

In jQueery it’s not .innerHTML() it’s.html(), but if you want text extracted from innerHTML you should use .text():

var tweet_quote = $('#quote').text();
var tweet_author = $('#author').text();

Yes, but keep them on one line :wink:


I think it should be something like this:

if (this is true){
else {

Thank you! It worked perfectly

ok I need some help… I trying to add twitter to my Randon quote Generator… 2 things… how can I get the button with the bird… and where do I put it…

and then how do I get the quote to show when I click on it…

keep in mind I am VERY VERY new here… and have trouble with this…

I have been researching all of these articles and not working

Hi Dennis–

Twitter makes their buttons available for you, and has an easy form to fill out, on their website here ( It generates some html that you can just copy into your code.

Where you put it is really up to you.

Insofar as getting the random quote to show up in the tweet, that takes a bit more work. There seems to be a few replies earlier in this thread that give fairly simple solutions. What I ended up doing was making it so the button that gave a new tweet on my website would also recreate the tweet button with the text of the quote. I stole that trick from a JSfiddle somewhere, so I feel obliged to share it as well…

  //twiiter button voodoo happens....
// Remove existing iframe
$('#tweetBtn iframe').remove();
// Generate new markup
var tweetBtn = $('<a></a>')
    .attr('href', '')
    .attr('data-url', 'e')
    .attr('data-text', A[R]["quote"] + " by " + A[R]["author"]);
  //this puts the button on the screen, i think

This is assuming that you’ve already got the button on your page, but this is the line you’re going to be concerned about: ->.attr(‘data-text’, A[R][“quote”] + " by " + A[R][“author”]); <- That’s obviously made to work for how I’ve defined my variables, but if you’ve made it this far, I think you can figure out how to make that work with what you’ve got.

Hope that helps.

Hi Dennismk1,
I know much time has passed since you posted your query for help. I faced the same invalid URL / 404 page not found problem.
The remedy described by ed-kahara above ( does the trick, when you encode the URI.
Hope you already figured that out, but this might help someone else! :slight_smile:

1 Like