HTML onplay Attribute

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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:



After clicking on play Button:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

After:

Supported Browsers:

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

full-stack-img




My Personal Notes arrow_drop_up

Recommended Posts:

    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.