Need help with Random Quote Generator Tweet

Need help with Random Quote Generator Tweet
0.0 0

#1

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: http://codepen.io/LorTos/pen/bwapvb


#2

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


#3

Hey
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 = ‘https://twitter.com/intent/tweet?text=’ + encodeURIComponent(tweet_quote + ’ \n’ + tweet_author)


#4
window.open("https://twitter.com/intent/tweet?text=" +

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.


#5

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


#6

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


#7

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:


#8

I think it should be something like this:

if (this is true){
do.something;
}
else {
do.something.different;
}


#9

Thank you! It worked perfectly


#10

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…

http://codepen.io/dennismk1/pen/BLYLVJ?editors=1010


#11

I have been researching all of these articles and not working


#12

http://codepen.io/dennismk1/pen/BLYLVJ


#13

Hi Dennis–

Twitter makes their buttons available for you, and has an easy form to fill out, on their website here (https://publish.twitter.com/#) 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....
ev.preventDefault();
// Remove existing iframe
$('#tweetBtn iframe').remove();
// Generate new markup
var tweetBtn = $('<a></a>')
    .addClass('twitter-share-button')
    .attr('href', 'http://twitter.com/share')
    .attr('data-url', 'e')
    .attr('data-text', A[R]["quote"] + " by " + A[R]["author"]);
  //this puts the button on the screen, i think
$('#tweetBtn').append(tweetBtn);
twttr.widgets.load();

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.


#14

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 (https://forum.freecodecamp.com/t/need-help-with-random-quote-generator-tweet/43728/3?u=aabhat2000) does the trick, when you encode the URI.
Hope you already figured that out, but this might help someone else! :slight_smile: