I've got a music player in which I can download audio files by linking the href directly to a click function. Each audio file is in a li element and everything works fine as long as I keep it hardcoded. But the moment that I convert the li elements and the link for download to dynamic, I can't connect the click function anymore. Can anyone help? Here's the code:
// Html hardcoded
<ul id="playlist1" class="hidden m-1 p-0">
<li id="li-items" data-song="the-deal.wav">The Deal<span class="dwn fa fa-download" onclick="downloadFile('/audio/the-deal.wav', 'The Deal')"></span></li>
<li id="li-items" data-song="rise-of-don.mp3">Rise of The Don<span class="dwn fa fa-download" onclick="downloadFile('/media/rise-of-don.mp3', 'Rise of The Don')"></span></li>
</ul>
// Audio Library js
var data1 = [{
href: "/media/the-deal.wav",
name: "The Deal",
song: "the-deal.wav"
}, {
href: "/media/rise-of-don.mp3",
name: "Rise of the Don",
song: "rise-of-don.mp3"
}]
// Create li elements
for (var i = 0; i < data1.length; i++) {
var t = document.createElement('li');
t.setAttribute("id","li-items");
var ta = document.createElement('span');
ta.classList.add("dwn","fa","fa-download");
ta.setAttribute("onclick","downloadFile('url','filename')"); // << This is the line that I'm having trouble with. 'Url' and 'filename' should be added dynamically for each li element.
t.dataset.song = s.song;
t.textContent = s.name;
t.appendChild(ta);
document.getElementById('playlist1').appendChild(t);
}
// Audio Player js - (download function):
function downloadFile(url, filename) {
//Filename download the user-defined name. The URL is the download address
var link = document.createElement('a');
link.href = url;
link.download = filename;
link.target = '_blank';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
link = null;
}
If anyone could help, I'd really appreciate it.
data-url="/audio/the-deal.wav". They will be easier to change and can be dynamic. Read them withelement.dataset.url