0

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] + '"); 
});   
2
  • 1
    Try this: jsfiddle.net/jw1sw01v/2 Concatenation was wrong! Commented Nov 30, 2015 at 9:11
  • 1
    $("#stage > iframe").attr("src", "https://www.youtube.com/embed/" + change[4]); Commented Nov 30, 2015 at 9:14

4 Answers 4

2

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

Comments

1

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]);
            
});   

Comments

1

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

Comments

1

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

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

Comments

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.