Global Variable Scope: Random Quote Generator


#1

Trying to set my Tweet link/button to pass the quote information to the tweet. I plan to pass the $.param(quote) but just wanted to pass the variable ‘quote’ for now that was set in the getJson call. However in my code it says that ‘quote’ is undefined.

$(document).ready(function(){
  var qodAPI = "https://random-quote-generator.herokuapp.com/api/quotes/random";
  var quote;
  var author;

  function displayQuote(data){
  quote = data.quote;
  author = data.author;

  $("#quote").html(quote + "<p>&mdash; " + author + "</p>")
  }

  function newQuote(){
    $.getJSON(qodAPI, displayQuote);
  }

  function tweetQuote(){
    $('#tweetButton').attr("href", "https://twitter.com/intent/tweet?text=" + quote)
  }

  //new quote when page loaded.
  newQuote();
  tweetQuote();

  $('#quoteButton').click(function(){
     newQuote();
  })

});

#2

Never mind. I moved setting up the tweet link into the callback function.