Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically get thumbnails and titles of vimeo videos

Picking up from this question: Get img thumbnails from Vimeo?

I'm trying to create a page with several vimeo videos on it.

I want to have the videos in the HTML look like this:

<div id='12345678' class='vimeo'></div>
<div id='23423423' class='vimeo'></div>

And then I want the jQuery to populate the divs with an img where the src is the thumbnail from vimeo, and an a which points to the video, and whose text is the title of the video. That is, it should end up like this:

<div id='12345678' class='vimeo'>
    <a href='https://vimeo.com/12345678'>
        <img src='url-to-thumbnail.jpg' />
        Video Title
    </a>
</div>

(Indentation added for clarity)

This is my starting point from that other question:

<script type="text/javascript">
    $.ajax({
        type:'GET',
        url: 'http://vimeo.com/api/v2/video/' + video_id + '.json',
        jsonp: 'callback',
        dataType: 'jsonp',
        success: function(data){
            var thumbnail_src = data[0].thumbnail_large;
            $('#thumb_wrapper').append('<img src="' + thumbnail_src + '"/>');
        }
    });
</script>

<div id="thumb_wrapper"></div>
like image 555
Dan Avatar asked May 15 '13 01:05

Dan


2 Answers

Try this:-

Assuming the div with id already exists.

  success: function(data){

              $('<a/>',{
                 href:data[0].url,
                 text:data[0].title }).append(
              $('<img/>' ,{
                 src:data[0].thumbnail_large
              })
              )
              .appendTo('#'+data[0].id)
        }

If the div with id doesnot exist:-

     success: function(data){

              $('<a/>',{
                 href:data[0].url,
                 text:data[0].title })
              .append(
                 $('<img/>' ,{
                 src:data[0].thumbnail_large
                 })
              ).appendTo($('<div/>',{
                 id:data[0].id,
                 class:'vimeo'

               }))
               .appendTo('.someContainer');
        }

Fiddle

like image 180
PSL Avatar answered Sep 21 '22 08:09

PSL


Append html to the corresponding div

var vimeo_content="<a>\
                        <img />\
                        <span></span>\
                    </a>";
// on success function
function(){
    var vdata = data[0];
    var thumbnail_src = vdata.thumbnail_large;
    var video_title   = vdata.title;
    var video_url     = vdata.url;

    $vimeo_content    = $(vimeo_content);
    $vimeo_content.find('a').attr('href',url);
    $vimeo_content.find('img').attr('src',thumbnail_src);
    $vimeo_content.find('span').text(title);
    //append to corresponding div
    $vimeo_content.appendTo($('div#'+video_id));
}
like image 40
unloco Avatar answered Sep 22 '22 08:09

unloco