Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

jQuery Generated RSS Feed Title & Twitter's Data-Text

Hello,

I am trying to dynamically change twitter's data-text attribute based on the title of an rss feed that I am generating (btw I hope to do the same thing with the feed's url and twitter's data-url tag). Here is my code:

<script>
        var j=jQuery.noConflict();
          j(document).ready(function() { 
              jQuery(function($) {
              j("#bird-feeds")
            .rss("http://feeds.feedburner.com/patheos/Ukoy", {
                         layoutTemplate: '<dl>{entries}</dl>',
                         entryTemplate: '<dt><a href="#feed{index}">{title}</a></dt><dd id="feed{index}"><p><a href="{url}" id="birdurl{index}" class="feedurl"><h2 id="birdfeed{index}">{title}</h2></a><p>{body}</p></p></dd>',
                      });
                                    });
               });
        </script> 
          <div id="bird-feeds">

      <script>
        $(document).ready(function() {    
          $('#bird-tweet[data-text]').each(function(){
          $(this).attr('data-text', $('#birdfeed0'));
          $.getScript('http://platform.twitter.com/widgets.js');
         });
        });
      </script>


      <a href="http://twitter.com/share"
        class="twitter-share-button" id="bird-tweet"
        data-text="This is what we want to change dynamically"
        data-count="none">Tweet</a>

When I click my button, [object Object] loads into my tweet. Moreover, if I change the text within my attribute jQuery call from the function thats present now to static text like, "this is a test," it works. To put this simply, how can I place the entire generated contents of this line (<h2 id="birdfeed{index}">{title}</h2>) into my data-text string?

Thanks in advance for your thoughts and efforts.

  • Joe

3 Answers

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

You'll need to use the jQuery data() method. First argument "text", second the value.

Within that line is

  $('#birdfeed0')

correct?

$(this).data('data-text', $('#birdfeed0'));
Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

You don't need the "data-" in front. What element is the "#birdfeed0" you may need to call .text() on it for it's contents.