Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding 'download' attribute to <a> dynamically using Jquery

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.

like image 995
Trevor Judice Avatar asked Jun 27 '16 18:06

Trevor Judice


1 Answers

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

like image 113
Korovjov Avatar answered Sep 23 '22 11:09

Korovjov