0

I'm using the toggle function to show and hide content, it works well, the only problem I have is that when I place a video the video continues to play even when it is hidden, is there any way to stop playing the video when it is hidden

$(document).ready(function(){
    $("button").click(function(){             
        $("p").toggle();
    });
});



 <button>Toggle</button>

  <p>the video is displayed here</p>
3
  • What about pause the video when the button is pressed? check that stackoverflow.com/questions/4155329/… Commented Nov 10, 2015 at 20:51
  • Are you using the html video tag to play your video? Commented Nov 10, 2015 at 20:52
  • Yes, I used the video tag Commented Nov 10, 2015 at 21:52

2 Answers 2

1

You can do:

$(document).ready(function(){
$("button").click(function(){
    $("p").toggle();
    if (document.getElementById('myVideo').paused) 
      document.getElementById('myVideo').play();
    else 
      document.getElementById('myVideo').pause();
});
});

If video is paused, play it and if it is playing then pause it on the click.

Sign up to request clarification or add additional context in comments.

1 Comment

Good solution, but what if the video is paused by the user, then the user clicks the toggle button to hide the video? This solution would hide the video, but resume playing it.
0

I think you would want to check if your wrapping p element is visible, using $("p").is(":visible").

If it is visible, pause the video using document.getElementById("yourVideoId").pause(), obviously replacing "yourVideoId" with whatever you have in the id attribute on your <video> tag. Then, after you do that, you can call .toggle() on your p element.

If it is not visible, and the user clicks your toggle button, I would imagine you would just want to show the video, and not resume playing it, as that might be a little jarring to the user. If you do want that functionality, however, just add document.getElementById("video").play() in the else block in the example below.

Here is a working example to demonstrate the functionality:

$(document).ready(function() {
    $("button").click(function() {
        var p = $("p");
        if (p.is(":visible")) {
          document.getElementById("video").pause();
          $(this).text("Show");
        } else {
          $(this).text("Hide");
        }
        p.toggle();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>
<video id="video" controls width="40%"> 
  <source src=http://techslides.com/demos/sample-videos/small.webm type=video/webm> 
  <source src=http://techslides.com/demos/sample-videos/small.ogv type=video/ogg> 
  <source src=http://techslides.com/demos/sample-videos/small.mp4 type=video/mp4>
  <source src=http://techslides.com/demos/sample-videos/small.3gp type=video/3gp>
</video>
</p>
<button>Hide</button>

Added width="40%" on the video so it fits in the snippet window.

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.