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

Asked

Viewed 55 times

0

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

codigo js

 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);

2 answers

0

Change in the first line:

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

If there is only one element with this class, everything will be fine. But if there is more than one, everything will go wrong.

0


Change the first line to:

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

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

  • Man, thank you so much, I had already put the Document.getElementsByClassName but I didn’t put the [0], so it didn’t work, now it’s working well, vlw :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.