Mathias Rechtzigel
Mathias Rechtzigel

Reputation: 3604

Firing an Event on <video> end with JQuery selector

I'm firing an event when a video is completed using:

video.addEventListener("ended", function() {
    //whatever
});

This works if I grab the selector via pure javascript: var video = document.getElementById('test');

This doesn't work if I grab the selector via jQuery: var video = $("#test");

Why is this the case? Here's the accompanying JSFiddle

Upvotes: 1

Views: 3589

Answers (2)

Samuel
Samuel

Reputation: 56

Thats because $("#test") returns a jQuery object. Use $("#test").get(0) to get the first dom element.

var video = $("#test").get(0);

Upvotes: 4

With jQuery, use the .on() method:

jqueryVideo.on("ended", function() {
    console.log('This Works 2');
});

http://jsfiddle.net/w4zahwk9/4/

Upvotes: 2

Related Questions