I want to use Javascript to make an anchor tag inside a button, so that, when I click this button, it will download a specified file I set before. But I don't know how to add attribute "download" when using Javascript create it.
function myFunction() {
var mydiv = document.getElementById("myDiv");
var aTag = document.createElement('a');
aTag.setAttribute('href',"abc/example.exe");
aTag.innerHTML = "<button>GO</button>";
mydiv.appendChild(aTag);
}
I want to use Javascript to make an anchor tag inside a button, so that, when I click this button, it will download a specified file I set before. But I don't know how to add attribute "download" when using Javascript create it.
function myFunction() {
var mydiv = document.getElementById("myDiv");
var aTag = document.createElement('a');
aTag.setAttribute('href',"abc./example.exe");
aTag.innerHTML = "<button>GO</button>";
mydiv.appendChild(aTag);
}
Share
Improve this question
asked Feb 8, 2018 at 16:39
Yuki WatayuYuki Watayu
931 gold badge1 silver badge4 bronze badges
2
-
1
aTag.setAttribute('download',"download");
– Scott Marcus Commented Feb 8, 2018 at 16:40 - God =)) Thanks so much. – Yuki Watayu Commented Feb 8, 2018 at 16:42
1 Answer
Reset to default 13download
is a Boolean attribute. That is, in HTML, no value is required to use it. The mere presence of the attribute is enough to make it work. Because of this, any value you might place on it isn't going to affect it working or not.
So, in situations like this, where you need to e up with a value for it, the remendation is to use the attribute name as the value, so your code would be:
aTag.setAttribute('download',"download");
Other examples of Boolean attributes are: disabled
and readonly
.