2

I'd like to populate the "data-text" attribute in Twitter's Tweet button with content from a DIV on my page:

<a href="http://twitter.com/share" class="twitter-share-button" data-text="">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>

<div id="populateText">
<p>Content from here to populate "data-text"</p>
</div>

Any ideas?

Yahreen
  • 75
  • 1
  • 4

2 Answers2

0

Try:

$(".twitter-share-button").attr("data-text", $("#populateText>p").text());

See it here: http://jsfiddle.net/Lv7rb/

James Wiseman
  • 29,946
  • 17
  • 95
  • 158
  • thanks, I see the alert but when I click the Tweet button I do not see the text populated: http://jsfiddle.net/Lv7rb/1/ – Yahreen Apr 06 '11 at 18:30
  • data-text is the text that you will be tweeting. Have a look at the API. That might be able to help you: http://dev.twitter.com/pages/tweet_button – James Wiseman Apr 06 '11 at 18:40
  • Right. I'm building a game which will have unique scores. So it's my hope that I can take the user's score (id="populateText") and have it show up as the text to tweet when the user clicks the "Tweet" button – Yahreen Apr 06 '11 at 18:46
  • Here's an example I just found doing the same thing I am attempting. I ran it through jsfiddle, however, and it doesnt appear to work. But it explains the problem: http://tumblr.christophercamps.com/post/4072517874/dynamic-tweet-button-text-using-jquery – Yahreen Apr 06 '11 at 18:54
-1
$(".twitter-share-button").data("text", $("#populateText p").text());
Adam Kiss
  • 11,811
  • 9
  • 48
  • 81
  • This won't work. `.data()` does *read* data- attributes on elements if they exist, but it doesn't write changes back through to the attributes when you set values through `.data()`. – Dave Ward Apr 06 '11 at 18:32