Stop a youtube video with jquery? Stop a youtube video with jquery? javascript javascript

Stop a youtube video with jquery?


This solution is simple, elegant and works in all browsers:

var video = $("#playerid").attr("src");$("#playerid").attr("src","");$("#playerid").attr("src",video);


from the API docs:

player.stopVideo()

so in jQuery:

$('#playerID').get(0).stopVideo();


1.include

<script type="text/javascript" src="http://www.youtube.com/player_api"></script>

2.add your youtube iframe.

<iframe id="player" src="http://www.youtube.com/embed/YOURID?rel=0&wmode=Opaque&enablejsapi=1" frameborder="0"></iframe>

3.magic time.

      <script>            var player;            function onYouTubePlayerAPIReady() {player = new YT.Player('player');}            //so on jquery event or whatever call the play or stop on the video.            //to play player.playVideo();            //to stop player.stopVideo();     </script>