Please help me to refresh a single html element after the initial page load. What my program should do in basic terms: (FreeCodeCamp exercise: Random Quote Machine)
- obtain random quote from API
- display quote and author
- button to obtain new quote/author
- button to tweet the quote/author
I have succeeded in making everything work as it should but I find that my anchor element that I use to send the tweet will not refresh after the initial page load. Its "data-text" attribute has changed accordingly but it does not reflect so on the button/link.
This is an extract from my html body: full code at http://codepen.io/jattas/pen/gmNQpv
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<div class="button-box">
<button id="getQuote">Generate new quote</button>
<a href="https://twitter.com/share" class="twitter-share-button" data-text="" data-show-count="false">Tweet</a>
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
</div>
I need the anchor element to refresh in order to display the newly changed attribute and not the one from the initial page load. Please help! I'm sure this is not supposed to be so difficult.
This is the relevant extract from my javascript:
var textToTweet = '';
function processQuote() {
var currentQuote = obj.quote;
var currentAuthor = obj.author;
$(".quote").html(currentQuote);
$(".author").html("- " + currentAuthor);
textToTweet = currentQuote + " " + "-" + currentAuthor;
$(".twitter-share-button").attr("data-text", textToTweet);
alert($(".twitter-share-button").attr("data-text")); //this is to confirm that the "data-text" attribute has changed as planned
}
$(document).ready(function() {
processQuote();
$("#getQuote").on("click", function() {
getQuote();
processQuote();
});
});
Many thanks.