I am trying to add the download attribute to an tag dynamically. Here is what I have so far:
$(fclass).append('<a href="/files/'+$days[$i][2][1]+'" download>'+$days[$i][2][0]+'</a>');
Which produces this:
<a href="/files/day0.pdf" download="">Slideshow (Notes)</a>
I also added the exact same tag as this produces into my HTML manually, but instead of
<a href="/files/day0.pdf" download="">
I used
<a href="/files/day0.pdf" download>
The manual version works perfectly, and automatically downloads the file, however, the dynamically placed version does not. I have also tried this:
$(a).attr("download","/files/day0.pdf");
But this does not seem to work. Any ideas on how to fix this? Thank you very much.
The answer is
$("a").attr("download", true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="file-to-download.txt">Download the file</a>
Just checked an it is working :D
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With