jQuery: a plugin to get your videos from YouTube

by gabrieleromanato

HTML

<div id="youtube"></div>

JavaScript

(function($) {

    $.fn.getYouTubeVideos = function(options) {
        var that = this;
        var settings = {
            items: 5,
            username: 'gabrieleromanato',
            width: 400,
            height: 300
        };
        options = $.extend(settings, options);

        var url = 'http://gdata.youtube.com/feeds/api/videos?q=' + options.username + '&max-results=' + options.items + '&v=2&alt=jsonc';

        return that.each(function() {

            var element = $(that);
            var html = '';

            $.getJSON(url, function(videos) {

                var items = videos.data.items;
                for (var i in items) {

                    var item = items[i];
                    var title = item.title;
                    var description = item.description;
                    var url = item.content['5'];

                    html += '<div class="video">';
                    html += '<h4>' + title + '</h4>';
                    html += '<iframe src="' + url + '" width="' + options.width + '" height="' + options.height + '"></iframe>';
                    html += '<p>' + description + '</p>';
                    html += '</div>';

                }
                element.html(html);
            });



        });

    };

})(jQuery);

$('#youtube').getYouTubeVideos();