Javascript update twitter Button

Viewed 88

I am working on creating a random quote generator. I have it mostly finished using quotesondesign.com's api, but the final thing I need to do is make the twitter button update to the new Quote when I click the "New Quote" button. But it wont change and I'm not sure why.

$( document ).ready(function() { 
  //get Quote And call to update twitter Button
  $.getJSON("https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&callback=", function(a) { 
    $("#quote-content").html(a[0].content); 
    $("#quote-title").text(a[0].title);
    updateTweet(a);
  });

  //new Quote and update Twitter
  $('#newButton').on('click', function() {
    $.ajax( {
      url: 'https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&callback=',
      success: function(a) {
        $('#quote-content').html(a[0].content);
        $('#quote-title').text(a[0].title);
        updateTweet(a);
      },
      cache: false
    });
  });

  //update twitter button function
  function updateTweet(a) {
    var thisQuote = a[0].content.replace(/<\/?[^>]+(>|$)/g, "");
    var thisQuoteTitle = a[0].title;
    $(".twitter-share-button").attr("href", "https://twitter.com/intent/tweet?text=" + thisQuote + "- " + thisQuoteTitle);
  }
});

Here is my codepen where I am working on this: https://codepen.io/Uberche/pen/RLbjbp

I realize it's ugly and not properly set up, just trying to get the functionality working before doing altering the style and such. Any help would be appreciated. Sorry if this is some silly thing I forgot to include, still learning javascript and forget things a lot...

2 Answers
Related