-2

How can I move from one variable called trench1 to trench2 with click I made this code but not working

var playthis = new Audio();
var trench1 = 'songs/example1.m4a';
var trench2 = 'songs/example2.m4a';
var trench3 = 'songs/example3.mp3';
i = 0;
$('.next').click(function(){
    playthis.src = trench + i++;
})
<button class="next"><i class="fas fa-caret-right"></i></button>

1 Answers1

1

You're going to want to use an array.

var trenches = [trench1, trench2, trench3];
var current_trench = 0;

$('.next').click(function() {
  playthis.src = trenches[current_trench++];
  if (current_trench >= trenches.length) current_trench = 0;
});
Michael Bianconi
  • 5,072
  • 1
  • 10
  • 25