I'm trying to download an image using the download
attribute on an <a>
tag. Its href
value is updated when it is clicked.
function Download_Image(element) {
var mydiv = $("#imageDiv");
var Image = mydiv[0].children;
mydiv.download = Image[0].src;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="imageDiv">
<img
width="200"
height="140"
src="https://2.bp.blogspot.com/-pjQRansD-ig/XAUEH2oe8tI/AAAAAAAAHwE/BgZOyaGnQLYhH2Zjxx86BRvyOs8o9yGjgCLcBGAs/s200/16e3d864-8fb8-4f43-b91e-cc97a75c602f.jpg" />
</div>
<a href="#" onclick="Download_Image(this)" download>Click here to download image</a>
However, when I click the link, it downloads the whole HTML page instead of the image.