I am creating an audio player in HTML using javascript and no jquery. I am doing so that it will work similar to this
I have the first table as a drop down list and I will select one of the albums and the corresponding songs will show in the next pane, this part is all working fine. My problem is that I don't know how to code it so that when I click on a song in the middle pane the song will show up in the last pane along with the corresponding image, album name and song name.
Here is my code with a shortened version of my var albums. I am also a beginner so do forgive if the code is messy.
<html>
<head>
<script type="text/javascript" src="music.js"></script>
<style type="text/css"></style>
</head>
<body>
<table width="400" height="400" border="1" style="display: inline-block;">
<caption>Albums</caption>
<tr>
<td><p>Please select an album from the list</p>
<select id="album-select" name='Album'>
<option></option>
</select>
</select>
</td>
</tr>
</table>
<table id="songs-table" width="400" height="400" border="1" style="display: inline-block;">
<caption>Songs</caption>
<tr>
</tr>
</table>
<table width="400" height="400" border="1" style="display: inline-block;">
<caption>
Selected Songs
</caption>
<tr>
<td>
<audio controls='controls'>
<source src='xxxxx.mp3' type='audio/mpeg'>
<source src='xxxxx.wav' type='audio/wav'>
Your browser does not support the audio element.
</audio>
</td>
</tr>
</table>
<script>
var albums=
[
{ "title":"Birdsong Small Birds",
"artist":"BBC",
"artwork":"Wren",
"tracks":[
{"title":"Dunnock","mp3":"Birdsong-Dunnock.mp3",
"lyrics":"The Dunnock or hedge sparrow has a fast warbling song often delivered from the top of a hedge or bush"},
{"title":"Robin","mp3":"Birdsong-Robin.mp3",
"lyrics":"Unusually among British birds, both the male and the femaale robins sing"},
var albumElement = document.getElementById('album-select');
albumElement.addEventListener('change', function(){
populateSongs(albumElement.value)
});
for(var i=0;albums.length;i++){
var option = document.createElement("option");
option.text = albums[i].title;
albumElement.add(option)
}
function populateSongs(album) {
var songsTable = document.getElementById('songs-table');
while(songsTable.rows.length > 0) {
songsTable.deleteRow(0);
}
for(var i=0;albums.length;i++){
if(albums[i].title == album) {
for(var track=0;albums[i].tracks.length;track++) {
var row = songsTable.insertRow(track);
var cell = row.insertCell(0);
cell.innerHTML = albums[i].tracks[track].title;
cell.setAttribute("file",albums[i].tracks[track].mp3);
cell.addEventListener('click',function(){
play(this.getAttribute('file'));
});
}
}
}
}
function play(song) {
var audioElement = document.getElementById('audio-player');
audioElement.src = song;
console.log(song);
}
</script>
</body>
</html>
Any help would be greatly appreciated.
i < albums.length. Those albums you can load as JSON files. Make function (or module) for last pane, and when user clicks on song, pass object containing all necessary information.xhr request. Its not that hard. When user clicks on second pane, make object with properties that you will need in third pane and pass it to function that will build third pane. (object example->var displaySong = { title: 'Album title', song: { title: 'Song title', ...) I can look your code, but you will need to send me all of it.