Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8263113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:59:17+00:00 2026-06-08T03:59:17+00:00

I am trying to extract the video ID that is contained within the URL

  • 0

I am trying to extract the video ID that is contained within the URL of a YouTube video and then store that ID in a variable that I can reference in creating an iframe to embed the YouTube video.

Below is my code:

  $(".result").live("click", function () {
       var result_number = 0;
       youtubequery = "https://gdata.youtube.com/feeds/api/videos?category=Music&q=thesongstitle&v=2&alt=json&max-results=5";
       youtubeid = "";
       $.getJSON(youtubequery, function (vid_data) {
           $.each(vid_data.feed.entry, function (e, vid) {
               if (e == result_number) {
                   youtubeid = vid.id.$t.substring(27);
               }
           });
       });
       iframehtml = "<iframe height='300' width='400' src='http://www.youtube.com/embed/" + youtubeid + "?autoplay=1' frameborder='0' allowfullscreen></iframe>";
       $("#youtube_vid").html(iframehtml);
   });

When I click on a div of the class “result” the first time the YouTube ID is not included in the iframe html stored in the variable iframehtml. But, when I click it the second time, everything works as expected.
Does anyone know what could be causing this?
I was able to get it to work when I included the creation of the iframehtml variable and the line that puts the html in #youtube_vid, but due to the functionality of my site I cannot code it that way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T03:59:19+00:00Added an answer on June 8, 2026 at 3:59 am

    your getJSON function is performing a asynchronous http get request. So when you call it, it returns before the data comes back from the request. By the time you click .result again the data has returned. try putting

    iframehtml = "<iframe height='300' width='400' src='http://www.youtube.com/embed/" + youtubeid + "?autoplay=1' frameborder='0' allowfullscreen></iframe>";
    $("#youtube_vid").html(iframehtml);
    

    inside your getJSON callback function, there will be a delay before the html shows, but it will work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to extract the video id from a youtube url using the
I am trying to extract the video id from the url http://www.youtube.com/v/Ig1WxMI9bxQ&hl=fr&fs=1&color1=0x2b405b&color2=0x6b8ab6 What i
I'm trying to extract/store href attribute from following anchor tag using xpath/css expression that
I am currently trying to extract the ID of a YouTube video from the
Im trying to extract the value of a single $_GET variable contained in a
I'm coding an app in Phonegap and I'm trying to extract the video-ID from
Trying to extract strings that are wrapped in double brackets. For example [[this is
I am trying to extract the facebook id from an iframe in order to
I am trying to extract the page name from a complex url. This post
I am trying to extract some parts of the Video: line from below text.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.