Open In App

HTML onplay Attribute

Improve
Improve
Like Article
Like
Save
Share
Report

The HTML onplay Attribute is a event attribute which occurs when the audio/video is played. The audio/video can be played either by the user or programmatically. 

Supported Tags: 

Syntax: 

<element onplay="myScript">

Attribute Value: This attribute contains single value script which works when onplay event attribute call. This attribute is supported by <audio> and <video> tags.

Example 1:

html




<!DOCTYPE html>
<html>
    <head>
        <title>
            HTML onplay Attribute
        </title>
    </head>
 
    <body>
        <center>
            <h1 style="color: green;">
                GeeksforGeks
            </h1>
            <h2>HTML onplay Attribute</h2>
 
            <audio controls id="audID"
                   onplay="GFGfun()">
                <source src=
                        type="audio/mpeg" />
            </audio>
 
            <script>
                function GFGfun() {
                    alert("audio play");
                }
            </script>
        </center>
    </body>
</html>


Output:

After clicking on play Button:

Example 2:

html




<!DOCTYPE html>
<html>
    <head>
        <title>
            HTML onplay Attribute
        </title>
    </head>
 
    <body>
        <center>
            <h1 style="color: green;">
                GeeksforGeks
            </h1>
            <h2>HTML onplay Attribute</h2>
 
            <video controls id="vidID"
                   onplay="GFGfun()"
                   width="320"
                   height="240">
                <source src=
                        type="video/mp4" />
            </video>
 
            <script>
                function GFGfun() {
                    alert("Video play");
                }
            </script>
        </center>
    </body>
</html>


Output:

After:

Supported Browsers: 
 

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Apple Safari
  • Opera

 



Last Updated : 01 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads