Call class="button" instead of id="download"

0

How do I make this script work with class="button" instead of id="download"?

js code

 var downloadButton = document.getElementById("download");
 var counter = 10;
 var newElement = document.createElement("p");
 newElement.innerHTML = "You can download the file in 10 seconds.";
 var id;

 downloadButton.parentNode.replaceChild(newElement, downloadButton);

 id = setInterval(function() {
counter--;
if(counter < 0) {
    newElement.parentNode.replaceChild(downloadButton, newElement);
    clearInterval(id);
} else {
    newElement.innerHTML = "You can download the file in " + 
 counter.toString() + " seconds.";
 }
 }, 1000);
    
asked by anonymous 30.09.2017 / 07:41

2 answers

0

Change the first line to:

var downloadButton = document.getElementsByClassName("button")[0];

Whereas the button is the first of the .button class. Even if there are others with the same class, only it will be selected.

    
30.09.2017 / 07:50
0

Change on the first line:

var downloadButton = document.querySelector(".button");

If there is only one element with this class, it will be all right. But if there is more than one, it will go wrong.

    
30.09.2017 / 07:44