Create Javascript Function Using Twitterlib.js
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Remy Sharp has an excellent little script that makes API calls to Twitter: https://github.com/remy/twitterlib

I've been able to write a function based on the samples that pulls the latest 3 tweets from a specified user and passes it to a div with the designated ID.

Your Task

The next step that I need it to go is to pull in the username from an attribute in the HTML.

So instead of:

<div id="tweet"></div>

I could add something like

<div id="tweet" data-username="bountify"></div>

And tada...the latest three tweets from @bountify would show up.

Make it so...

Are you fine with jQuery?
alex over 9 years ago
Sure. I'm fine with that.
renwalker over 9 years ago
awarded to alex

Crowdsource coding tasks.

3 Solutions

Winning solution

A demo is available here. You can download the files here.

I grabbed the data-username using jQuery and used it as a variable.

    <script>
    function log(text, time) {
        document.getElementById('tweet').innerHTML += "<p>" + text + "<i>" + time + "</i></p>";
    }

    $(document).ready(function() {
        var username = $("#tweet").data("username");

        twitterlib.timeline(username, { limit: 3 }, function (tweets) {
            for (var i = 0; i < tweets.length; i++) {
                log(this.ify.clean(tweets[i].text), tweets[i].created_at);
            }
        });
    });
    </script>
Great! In my original function, I had the relative time of the Tweet. I see this version brings in tweets[i].text, but can I also add the date from the API?
renwalker over 9 years ago
@renwalker Did you want the time the tweet was created at or the relative time? (I updated it with the created at time).
alex over 9 years ago
AMAZINGLYTON. Thanks, Alex!
renwalker over 9 years ago
@renwalker Anytime!
alex over 9 years ago
View Timeline