View video tutorial

HTML onpause Attribute

HTML

The HTML onpause attribute is an event handler used to specify a script that will run when an <audio> or <video> element is paused.

Definition and Usage


➔ The user can initiate the pause using media controls or programmatically.

➔ onpause is the opposite of the onplay event attribute.

Syntax
//In HTML
<video controls onpause="myFunction()" id="sample">
        <source src="myvideo.mp4" type="video/mp4">
</video>
//In javascript
document.getElementById("sample").onpause=function() {myFunction();};
//OR
document.getElementById("sample").addEventListener("pause", myFunction);
                            

Sample
<html>
<head>
    <title>HTML onpause attribute example</title>
</head>
<body>
    <h3>HTML onpause attribute example</h3>
    <p>Set the src correctly and play/pause to trigger the event.</p>
    <!-- "https://mdn.github.io/learning-area/html/multimedia-and-embedding/video-and-audio-content/rabbit320.mp4" -->
    <video controls onpause="myFunction()" id="sample">
        <source src="myvideo.mp4" type="video/mp4">
        This browser does not support the video tag.
    </video>
    <script>
        //In javascript
        //document.getElementById("sample").onpause=function() {myFunction();};
        //OR
        //document.getElementById("sample").addEventListener("pause", myFunction);
        function myFunction() {
            alert("The video has been paused!");
        }
    </script>
</body>
</html>

Applies to

This attribute can be used on the following element.

Attribute Element
onpause <audio>, <video>