I'm creating video objects via jquery from an array of urls that point to mp4 files. I want to have the first video autoplay then once its finished pick up on the "ended" function and play the next video in the sequence.
 $(document).ready(function () {
            var urls = ["https://s3-us-west-1.amazonaws.com/linkto.mp4", "https://s3-us-west-1.amazonaws.com/linktoother.mp4"]
            var videos = [];
            for (var i = 0; i < urls.length; i++) {
                var path = urls[i];
                var video = $('<video></video>');
                var source = $('<source></source>');
                source.attr('src', path);
                source.appendTo(video);
                if (i === 0) {
                    video.attr('autoplay', 'true');
                }
                video.attr('id', 'video_' + i);
                video.attr('preload', 'true');
                video.attr('width', '100%');
                videos.push(video);
                video.on('ended', function () {
                    console.log('Video has ended!' + i);
//                        playNext(video);
                });
                $('#movie').append(video);
            }
I can see the movies generated and the first one plays fine. The problem is when 'ended' function the variable i == 2 because thats how many videos are in the array and the for loop increased i. I've been away from javascript for a while. I can think of workarounds like adding an index attribute to the video element or something but basically how do I get the correct index in the "ended" function.
 
     
    