Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  
  <button>Play random audio</button>  
  <audio id="audio"></audio>
</body>
</html>
 
var audio = ["audio-1.mp3", "audio-2.mp3", "audio-3.mp3", "audio-4.mp3", "audio-5.mp3"];
var $btn = $('button');
var $audio = $('audio');
function getRandomAudio() {
    var max = audio.length;
    return Math.floor(Math.random() * (max + 1));
}
$btn.on('mouseover', function(){
  var index = getRandomAudio();
  $audio.attr('src', audio[index]);
  $audio.get(0).play();
});
Output 300px

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers