0

I'm trying to pull in view counts for three different youTube videos. I've looked all over, and I can't seem to find what I'm doing wrong.

I followed this question: Get Youtube information via JSON for single video (not feed) in Javascript

And using the script from there, I was able to get the viewCount for the video popping up in an alert, but I still am not exactly sure how to create a success handler that sends the viewCount information into a paragraph.

$(document).ready(function() {

var video_id='VA770wpLX-Q';

$.getJSON('http://gdata.youtube.com/feeds/api/videos/'+video_id+'?v=2&alt=jsonc',function(data,status,xhr){
alert(data.data.viewCount);
});

});

Here's a fiddle of what I have so far: http://jsfiddle.net/wqwxg/199/

Community
  • 1
  • 1
Eric Wood
  • 579
  • 2
  • 9
  • 22

1 Answers1

1

JavaScript:

$(document).ready(function() {
    var video_id='VA770wpLX-Q';

    $.getJSON('http://gdata.youtube.com/feeds/api/videos/' + video_id + '?v=2&alt=jsonc', function(data, status, xhr) {
        $('#targetId').text(data.data.viewCount);
    });
});

HTML:

<p id="targetId"></p>
rgtk
  • 3,240
  • 1
  • 30
  • 36
  • Thanks so much! This is awesome. How would I go about adding in comments for every three digits? ie. 1,540,110 views. – Eric Wood Aug 29 '13 at 16:29
  • Ask another question about that. In StackOverflow we solve one problem per question. Also, remember to mark this answer as accepted. – rgtk Aug 29 '13 at 19:57