Unable to change attribute of twitter tweet button using jQuery

It is a challenge in freecodecamp where user has to create random quote machine and as bonus problem it should also tweet selected quote. When I am trying to change ‘data-text’ attribute using .attr() method of jQuery, it is not changing.
My code is in codepen
In following line of jQuery code I am trying to change attribute but it is not changing.

$('#twitter').find("a").attr("data-text",quote[randomNum]);

What could be the possible solution for this?

Try printing out the <a> tag you’re trying to modify:

console.log($('#twitter').find("a")[0])

I think you’ll find that it’s not there. The link gets replaced by an iframe that contains the Twitter button by the script you pasted at the bottom.

To fix that, you probably need to recreate the link every time you want to change the tweet text.

1 Like