Reputation: 1559
I am working with embeded youtube video. I need to replace it with the image and when the image is clicked the video starts playing which I have implemented:
<script type="text/javascript">
function start_video() {
var iframe = '<iframe title="YouTube video player" width="590" height="360" src="http://www.youtube.com/embed/nCgQDjiotG0?autoplay=1&rel=0&showinfo=0" frameborder="0" title="none" allowfullscreen></iframe>';
document.getElementById("video_player").innerHTML = iframe;
}
</script>
And html:
<div id="video_player"><img style="cursor: pointer;" onclick="start_video();" src="fake_image.jpg" alt="Play Video" /></div>
But now I have a problem stopping this video by clicking on some div or some other element and replacing video back with image at the same time.
Upvotes: 2
Views: 1547
Reputation: 7785
Why don't you just give the iframe an id and then remove the iframe and place the image there?
function stop_video() {
var elem = document.getElementById('video_iframe');
var parent = elem.parentNode;
parent.removeChild(elem);
var image = document.createElement('img');
image.setAttribute('src','yourImage.jpg');
parent.appendChild(image);
}
I also suggest that you do not set the innerHTML as you did, you should actually deal with objects like this one above.
function start_video() {
var iframe = document.createElement('iframe');
iframe.setAttribute('title','Youtube video player');
iframe.setAttribute('width','590');
iframe.setAttribute('height','360');
iframe.setAttribute('src','http://www.youtube.com/embed/nCgQDjiotG0?autoplay=1&rel=0&showinfo=0');
iframe.setAttribute('frameborder','0');
iframe.setAttribute('allowfullscreen','allowfullscreen');
document.getElementById('video_player').appendChild(iframe);
}
Upvotes: 1