Question

Given these two variables:

var all = $('audio');
var specific = $(this);

Where all includes specific, how do I find the element that comes after specific in the all collection?


Purpose: When one audio element ends playing, I want to start the next one automatically.

$('audio')
    .each(function(){
        this.onended = onEnded;
    });

function onEnded(event)
{
    var next = ?;
    next.play();
}
Était-ce utile?

La solution

I'd suggest using index() with a selector to match:

var current = specific.index('audio'),
    next = all.eq(current + 1);

Or, to loop back to the first if there is no 'next' audio element:

    next = all.eq(current + 1 < all.length ? current + 1 : 0);

References:

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top