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/

1
  • You mean like this ? Commented Aug 29, 2013 at 15:17

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much! This is awesome. How would I go about adding in comments for every three digits? ie. 1,540,110 views.
Ask another question about that. In StackOverflow we solve one problem per question. Also, remember to mark this answer as accepted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.