Eval() is not working in FireFox

In the following piece of code, execution of eval() is not working in FireFox.
Pls provide the suggestions.

var str="audioElement[0].play();";

for(i=0;i<fileNames.length-1;i++)
{
			
str = str + "audioElement[" + i + "].addEventListener('ended', function() {audioElement[" + (i+1) + "].play();}, false);";
}

str = str + "audioElement[" + (fileNames.length-1) + "].addEventListener('ended', function(){}, false);";
eval(str);

Here, filenames array is having the array of HTML5 audio tag elements.

“Eval is evil” - learn that and you won’t go far wrong.

Here’s the code without the eval garbage.


function play(audio) {
    return function() {
        audio.play();
    };
}

audioElement[0].play();

for (i = 0; i < fileNames.length - 1; i++) {
    audioElement[i].addEventListener('ended', play(audioElement[i + 1]), false);
}
audioElement[fileNames.length - 1].addEventListener('ended', function(){}, false);

It’s not working. Audio got executed for only first file.

Perhaps due to a small typo.

This “a” should be lowercase.

Ya…! I have taken care.