I need your help. I have got a download button with a countdown but there is an issue with the button. It only works on First Button or if when clicked on 2nd button first, the starts on 1st button instead of 2nd button. Please help me to fix this error. I have created a snippet with the coding. Please review it and let me know, what is wrong! Thank you.
function generate() {
var e, n = document.getElementById("downloadingx"),
t = document.getElementById("downloadbuttonx"),
a = document.getElementById("downloadingx").href,
l = 10,
d = document.createElement("span");
n.parentNode.replaceChild(d, n), e = setInterval(function() {
--l < 0 ? (d.parentNode.replaceChild(n, d), clearInterval(e), window.open(a), n.style.display = "inline") : (d.innerHTML = "<i class='fa fa-clock-o' aria-hidden='true'/> Redirecting to download page in " + l.toString() + " seconds.", t.style.display = "none")
}, 1e3)
}
#downloadbuttonx{
cursor: pointer;
padding: 10px 20px;
border: none;
border-radius: 3px;
background: #fff;
color: #e67e22;
float: none;
text-transform: uppercase;
font-weight: 800;
transition: all 0.5s;
font-size: 16px;
outline: none;
}
#downloadbuttonx:hover,
#downloadingx:hover{
background: #ffffff;
color: #b62727;
outline: none;
}
.batas-downx{
margin: auto;
border-radius: 4px;
display:block;
}
.background-box-st{
background: #b62727;
color: #fff;
padding: 10px 10px 5px 10px;
width: 310px;
text-align: center;
left: 50%;
transform: translate(-50%);
position: relative;
border-top-right-radius:5px;
border-top-left-radius:5px;
border-bottom-right-radius:5px;
border-bottom-left-radius:5px;
line-height: 80px;
margin-bottom: 10px;
}
.background-box-n{
background: #a30085;
color: #fff;
padding: 10px 10px 5px 10px;
width: 310px;
text-align: center;
left: 50%;
transform: translate(-50%);
position: relative;
border-top-right-radius:5px;
border-top-left-radius:5px;
border-bottom-right-radius:5px;
border-bottom-left-radius:5px;
line-height: 80px;
margin-bottom: 10px;
}
.file-name{
font-family: sans-serif;
font-size: 1.3em;
font-weight: 700;
color: #fff;
line-height: 35px;
text-align: center;
display: block;
margin: 5px;
}
.catatan-downx{
padding:20px;
background:#f7f7f7;
color:#555;
font-size:20px;
}
#downloadingx{
display:block;
padding: 10px 20px;
border-radius: 3px;
color: #e67e22;
background: #fff;
text-decoration: none;
text-transform:capitalize;
text-align:center;
float:none;
line-height:80px;
font-size:16px;
font-weight: 600;
}
.bungkus-info span{
display:block;
line-height:80px;
float:none;
}
.file-deskripsi{
display:block;
}
.file-deskripsi span{
margin-right:3px;
}
#downloadbuttonx,
a#downloadingx{
width:160px;
padding:15px;
cursor:pointer;
}
.bungkus-info span{
float:none;
width:100%;
text-align:center;
}
.file-deskripsi{text-align:center}
}
<div class="batas-downx">
<div class="background-box-n">
<div class="bungkus-info">
<div class="file-name">File 1</div>
<button id="downloadbuttonx" onclick="generate()"><i aria-hidden="true" class="fa fa-cloud-download"></i> Download</button>
<a href="#" id="downloadingx" style="display: none;"><i aria-hidden="true" class="fa fa-cloud-download"></i> Redirecting...</a>
</div>
</div>
</div>
<br>
<div class="batas-downx">
<div class="background-box-st">
<div class="bungkus-info">
<div class="file-name">File 1</div>
<button id="downloadbuttonx" onclick="generate()"><i aria-hidden="true" class="fa fa-cloud-download"></i> Download</button>
<a href="#" id="downloadingx" style="display: none;"><i aria-hidden="true" class="fa fa-cloud-download"></i> Redirecting...</a>
</div>
</div>
</div>