I'm learning Javascript and i need some help. I have a list. I've tried to make a list, where you can, by the click of a button, get a random song from that list, but it doesn't seem to work. My list is down below, what am i doing wrong?
<!DOCKTYPE html>
<html>
<head>
</head>
<body>
<div>
<button type="randomSong">Random Music</button>
<input "randomSong" id="randomSong">
</div>
<script>
var song = Array("song1", "song2", "song3", "song4", "song5", "song6");
var randomSong = song[Math.floor(Math.random()*song.length)];
</script>
</body>
</html>
Your code is almost correct. Here is a proper version:
HTML
<div>
<button type="randomSong" onclick="randomSong()">Random Music</button>
<input name="randomSong" id="randomSong">
</div>
Modifications:
"randomSong"
without any attribute key)onclick
callback, so that something happens when you click your buttonJS
var song = Array("song1", "song2", "song3", "song4", "song5", "song6");
function randomSong() {
var randomSong = song[Math.floor(Math.random() * song.length)];
document.getElementById('randomSong').value = randomSong;
}
Modifications:
onclick
attribute of your button)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With