Tweet button quote generator Help

Tweet button quote generator Help
0.0 0

#1

I have no idea where to start and all of the forums that I looked at about this subject are specific to their code. Can someone point me to the right direction. Here is what I have so far


#2

That’s funny, I’ve answered this question like 4 times in the last couple days including this morning.

Anyway, this is the URL you will need to use:

 https://twitter.com/intent/tweet?text=[yourQuote]

Assuming you have the quote as a variable, you can easily add it to the url with a template string:

 `https://twitter.com/intent/tweet?text=${quote}`

You will need to open this link when they click on your tweet button, so find a way to put that link as the tweet button’s href.

Example:
https://twitter.com/intent/tweet?text=A Wonderful Message


#4

Hey, I checked your code, you’re almost there! I see you got the tweet button on your page, and it’s going well.


I don’t think you are aware of what a template literal is, you have this as your link:

window.open('https://twitter.com/intent/tweet?text=${getQuote()}');

That is basically an exact string. You can use the + operator to add a variable to a string, so that would be like this:

window.open('https://twitter.com/intent/tweet?text=' + getQuote());

I used what is called a template literal. Template literals allow us to use ${} to insert variables into a string. To use template literals, you must use backticks not a singlequote. What you had would be a great template literal, but change the single quotes to a backtick:

window.open(`https://twitter.com/intent/tweet?text=${getQuote()}`);

Once you get that, your link will be mostly good. There are a couple other slight problems that you can change to make your code work better.

You don’t need to use Javascript to open the window like you are doing:

$("#tweet").on("click", function(){
   
    window.open( `https://twitter.com/intent/tweet?text=${getQuote()}`);
    
});

You can modify your existing tweet button’s href inside the getQuote function. Get rid of the tweet button’s click handler, it is a button already. Move your logic to the bottom of getQuote. You can simply modify your twitter button’s href with the new link, and the variables are there for you since you are inside your getQuote() function:

$('#tweet').attr('href', `https://twitter.com/intent/tweet?text=${randomQuote} by ${randomAuth}`);

Make sure you add target="_blank" to your button, and you have a working solution, congrats!

You can see the working changes I made here: https://codepen.io/isaacabrahamson/pen/wmpObq?editors=1010


#5

I got it to work Thank you! and thank you for taking the time out of your day to help me out, it is much appreciated :pray: