Adding the 'download' attribute to <a> dynamically with jQuery

I am trying to add a loading attribute to a tag dynamically. Here is what I still have:

$(fclass).append('<a href="/files/'+$days[$i][2][1]+'" download>'+$days[$i][2][0]+'</a>'); 

What produces this:

 <a href="/files/day0.pdf" download="">Slideshow (Notes)</a> 

I also added the same tags as in my HTML, but instead

 <a href="/files/day0.pdf" download=""> 

I used

 <a href="/files/day0.pdf" download> 

The manual version works fine and automatically downloads the file, however the dynamically placed version does not work. I also tried this:

 $(a).attr("download","/files/day0.pdf"); 

But this does not seem to work. Any ideas how to fix this? Thank you very much.

+5
source share
2 answers

Answer

 $("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 that it works: D

+3
source

Have you tried to use the details?

 $(a).prop("download","/files/day0.pdf"); 
-1
source

All Articles