1

I am trying to fade in an icon when video finish, but nothing.

Html

<video>
    <source src="">
</video>
<div class="icon"></div>

First Try

$("video").on("ended", function() { 
       $(".icon").fadeIn;
});

Second try

$('video').parent().on("ended", function() {
    if($(this).children("video").get(0).paused) { 
        $(this).children(".icon").fadeIn();
    }
});

Actually has onclick event working fine

$("video").parent().on("click", function() {         
        $(this).children(".icon").fadeToggle(); 
});
Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
Jose
  • 117
  • 1
  • 8
  • 1
    Does this answer your question? [Detect when an HTML5 video finishes](https://stackoverflow.com/questions/2741493/detect-when-an-html5-video-finishes) – dryleaf Jan 12 '20 at 12:53
  • i always get "Uncaught TypeError: Cannot read property 'addEventListener' of null" in console with this solution, maybe i am doing something wrong – Jose Jan 12 '20 at 13:15

2 Answers2

0

You can try this way:

<video id="myVideo">
    <source src="">
</video>
<div class="icon"></div>

var vid = document.getElementById("myVideo");
vid.onended = function() {
    alert("The video has ended");  //Just to verify if the event is trapped
    $(".icon").fadeIn();
};
Nicola Lepetit
  • 756
  • 8
  • 25
  • I think its fadeIn(). Anyway its working fine if i use getElementById, but if i use getElementsByClass doesnt work, or if i use getElementByTagName. Anyway why my code doesnt work? i cant understand it. – Jose Jan 12 '20 at 13:47
0

You can achieve it in this way

    document.getElementById('video').addEventListener('ended',function() {
           $(".icon").fadeIn();
    },false);
Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56