m0a
m0a

Reputation: 1005

Trying to switch URL of iframe YouTube video on click with jQuery

I have two divs. One contains a YouTube iframe video and the other contains a YouTube thumbnail. When the button is pressed, these are the intended steps:

-retrieve URL of the thumbnail

-replace URL of the iframe video with the URL of the YouTube video the thumbnail is from.

Here is the JSFiddle. It seems like it should work, but when I click the button I'm only getting a black screen. Can anyone offer advice? http://jsfiddle.net/jw1sw01v/1/

HTML

<div id="stage">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/e-ORhEE9VVg" frameborder="0" allowfullscreen></iframe>
</div>


<div class="slideYThumbnail">
        <img class="slideYThumbnailInside" src="http://img.youtube.com/vi/kt0g4dWxEBo/0.jpg">
        </img>
</div>

jQuery

$(document).on('click', '.slideYThumbnail', function(event) {
        var change = $(this).find("img").attr("src").split("/");
        alert(change[4]);
        $("#stage > iframe").attr("src", "https://www.youtube.com/embed/' + change[4] + '"); 
});   

Upvotes: 0

Views: 2776

Answers (4)

Muhammad Zohaib Yunis
Muhammad Zohaib Yunis

Reputation: 536

for directly on image

$(document).on('click', '.slideYThumbnailInside', function (event) {
    var change = $(this).attr("src").split("/");
    $("#stage > iframe").attr("src", "https://www.youtube.com/embed/"change[4]);
            
});   

Upvotes: 1

JohnnyP
JohnnyP

Reputation: 500

Remove the single quotes from your new video source concatenation like so:

 $("#stage > iframe").attr("src", "https://www.youtube.com/embed/" + change[4]);

Upvotes: 1

Chonchol Mahmud
Chonchol Mahmud

Reputation: 2735

Add this js instead of yours JS:

$(document).on('click', '.slideYThumbnail', function (event) {
  var change = $(this).find("img").attr("src").split("/");
  alert(change[4]);
  $("#stage > iframe").attr("src", "https://www.youtube.com/embed/"change[4]);
});

Working Link

Upvotes: 1

Ray
Ray

Reputation: 777

you have to remove single quote in js

$(document).on('click', '.slideYThumbnail', function(event) {
    var change = $(this).find("img").attr("src").split("/");
    alert(change[4]);
    $("#stage > iframe").attr("src", "https://www.youtube.com/embed/" + change[4] ); 
});   

Upvotes: 2

Related Questions