-1

Why aren't events being binded in my simple JQuery code? - http://pastebin.com/24J6q5G0

Relevant sections:

<div class="videoWrapper">
    <video controls preload autoplay>
        <source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
        <source src="http://techslides.com/demos/sample-videos/small.ogv" type="video/ogg">
    </video>
</div>

<div class="videoWrapper">
    <video controls preload="none">
        <source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
        <source src="http://techslides.com/demos/sample-videos/small.ogv" type="video/ogg">
    </video>
</div>

And the javascript:

$(document).ready(function() {
    var vid0 = $(document.getElementsByClassName('videoWrapper')[0]);
    var vid1 = $(document.getElementsByClassName('videoWrapper')[1]);
    vid1.hide();
    vid0.bind('ended', function() {
        vid1.show();
    })
})

(I have been following along with Detect when an HTML5 video finishes)

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
stackoverflowuser95
  • 1,992
  • 3
  • 20
  • 30

4 Answers4

2

The ended event is not propagated(bubbled), so try

$(document).ready(function() {
    var wrappers = $('.videoWrapper');
    wrappers.eq(1).hide()
    wrappers.eq(0).find('video').bind('ended', function() {
        wrappers.eq(1).show();
    })
})

In your case you are binding the event to the wrapping div element, but the ended event is not bubbled to the parent element which means your handler will never get executed.

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

Firstly, the events for HTML5 video fire on the video element, so you're attaching to the wrong element. Also, your'e method of creating jQuery selectors is a bit odd. Try this:

var $vid0 = $('.videoWrapper video').eq(0);
var $vid1 = $('.videoWrapper video').eq(1);
$vid1.parent().hide();
$vid0.bind('ended', function() {
    $vid1.parent().show();
})

Example fiddle

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
-1

The reason it's not working is because your vid0 is the wrapper div and the ended event needs the video tag.

Here is a working demo fix: http://jsfiddle.net/

I added #video0 and #video1 to the video tag.

Miro
  • 8,402
  • 3
  • 34
  • 72
-1

The solution I used (think there was only one [wrong] answer at the time) was:

$(document).ready(function() {
    var vid0 = $(document.getElementsByClassName('videoWrapper')[0]);
    var vid1 = $(document.getElementsByClassName('videoWrapper')[1]);
    vid1.hide();
    vid0.children().bind('ended', function() {    // Note this line
        vid1.show();
    })
})
stackoverflowuser95
  • 1,992
  • 3
  • 20
  • 30