javascript - set download attribute on dynamically displayed image -


i'm displaying long list of images site on page below code. id able use download html5 attribute click each image download it.

here's i've tried:

for (var = 0; i<arlndata.d.length;i++) {         var img = document.createelement('img');         img.src = "https://images.website.com/airvendors/airlines_"+arlndata.d[i].code+".gif";          img.download ="my image";           //also tried:          //img.src = "https://images.website.com/airvendors/airlines_"+arlndata.d[i].code+".gif";         document.body.appendchild(img);          var imagecellspace=document.createelement('br');         document.body.appendchild(imagecellspace); } 

images displayed fine clicking download doesnt work.

what proper syntax here?

try this:

for (var = 0; i<arlndata.d.length;i++) {         var img = document.createelement('img');         img.src = "https://images.website.com/airvendors/airlines_"+arlndata.d[i].code+".gif";           var = document.createelement('a');         a.href = img.src;         a.download = 'image.gif';          a.appendchild(img);          document.body.appendchild(a);          var imagecellspace=document.createelement('br');         document.body.appendchild(imagecellspace); } 

the download attribute a, not img. check documentation.


Comments