qadenza
qadenza

Reputation: 9293

image.jpg instead of true.html in save dialog box

I don't know how to add image in my snippet code but hope the question will bi clear anyway.

Want to save i.e. download imgb on disk.

Save As dialog appears but:

Any help?

$('button').on('click', function(){
  let dl = document.createElement("a");
  dl.href = document.getElementById('imgb');
  dl.download = true;
  document.body.appendChild(dl);
  dl.click();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img class='imgb' id='imgb' src='img.jpg' alt='img'>
<br><br>
<button>CLICK</button>

Upvotes: 0

Views: 23

Answers (1)

Daniel Kemeny
Daniel Kemeny

Reputation: 690

I think this will work:

 $('button').on('click', function(){
      let dl = document.createElement("a");
      dl.href = document.getElementById('imgb').getAttribute("src");
      dl.download = true;
      document.body.appendChild(dl);
      dl.click();
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

html:

    <img id='imgb' src='img.jpg' alt='img'>
    <br><br>
    <button>CLICK</button>

Your element hat the class of imgb and not id, and you can access the href by using getAtribute

Upvotes: 1

Related Questions