Skip to content

Instantly share code, notes, and snippets.

@hlorand
Created September 24, 2017 18:24
Show Gist options
  • Select an option

  • Save hlorand/d79d02ae6c99a87539892dca39d4eee1 to your computer and use it in GitHub Desktop.

Select an option

Save hlorand/d79d02ae6c99a87539892dca39d4eee1 to your computer and use it in GitHub Desktop.
jQuery: Play Stop YouTube video
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
function stopvideo(id){
var videoid = "#video" + id;
var video = $(videoid).attr("src");
var video = video.replace("?autoplay=1", ""); //ha autoplay-es akkor azt is kikapcsoljuk
$(videoid).attr("src","");
$(videoid).attr("src",video);
}
function playvideo(id){
var videoid = "#video" + id;
var video = $(videoid).attr("src") + "?autoplay=1";
$(videoid).attr("src","");
$(videoid).attr("src",video);
}
</script>
<a href="#!" onclick="playvideo(1);">play</a>
<a href="#!" onclick="stopvideo(1);">stop</a>
<iframe id="video1" width="560" height="315" src="https://www.youtube.com/embed/IFXIGHOElrE" frameborder="0" allowfullscreen></iframe>
<br>
<a href="#!" onclick="playvideo(2);">play</a>
<a href="#!" onclick="stopvideo(2);">stop</a>
<iframe id="video2" width="560" height="315" src="https://www.youtube.com/embed/0CBQI7cktMk" frameborder="0" allowfullscreen></iframe>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment