The solution I went with uses the iframe Tweet Button, and basically you just need a bit of javascript to manipulate the URL that configures the iframe Tweet Button. I went with the iframe Tweet Button over the javascript Tweet Button because the javascript one manipulates the DOM and replaces your anchor tag with an iframe, etc. You could theoretically dynamically change the tweet text for the javascript Tweet Button but it would be a little brittle, being dependent on how the javascript Tweet Button renders itself.
Enough jibber jabber, on to the code! You start by adding an iframe Tweet Button to the page:
Then you need this function which updates the text parameter of the iframe Tweet Button's query string (which of course will cause the button to reload):
You can try it out right here. Click on the Tweet Button below and preview the message, which should be "Replace Me":
Now click on the button below which will call
updateTweetButtonText('I just dynamically updated the text of a Tweet Button!');
and then click on the Tweet Button again to see that the preview tweet has been updated:
3 comments:
Hey thanks for this man, i was trying to do the same thing but with jquery using...
$('#tweet-button').attr('src', tweet_text);
but it wasn't having it. This good old Javascript way works great.
Thanks, this was really helpful in a time of need!
Your exmaple is not running any more... I think Twitter has updated its code.
Post a Comment