Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

343
Views
Download QR code generated using Javscript

I'm using this library which generates QR codes as PNG images: https://davidshimjs.github.io/qrcodejs/

I want to have a download button for the generated QR code, however the canvas only appear when the image is generated. Any tips on how to put a download button?

Here's my sample code for my project:

PHP:

<div>
    <!-- <input type="text" id="qr-data" value="<?php echo uniqid();; ?>"/> -->
    <button onclick="generateQR()">Generate</button>
    <div id="qrcode"></div>
    <script src="qrcode.min.js"></script>
</div>

Javascript:

<script>
    var qrdata = document.getElementById("qr-data");
    var qrcode = new QRCode(document.getElementById("qrcode"));

    function generateQR() {
        var data = qrdata.value;
        qrcode.makeCode(data);
    }
</script>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

A quick search turns up a simple way to download an image when clicking on it:

  • we need a link;
  • with a download attribute;
  • with the same href as the image src;

The image src we're after doesn't exist until the code is generated. So we need to dynamically copy it once it is created. So we'll need to add a download link/button to your HTML, and to copy the QR code src to the link href after it is generated.

So first up, add the link:

<a id="download" href="" download="qr.png"><button>Download</button></a>

Next, we need to copy the generated src. One catch is that we need to wait for the QR code to be generated before we can copy it. Trying to do the copy immediately after your makeCode() line does not work, because makeCode() takes some time to run, and runs asynchronously. That means code immediately after that line will run before makeCode() has finished - we can't copy the src before the src exists!

There are no documented success- or done-type events or callbacks for QRCode.js, so the safest option seems to be to do it later, when the download button is clicked. So add an event listener for our download link:

var link = document.getElementById("download");
link.addEventListener("click", setUpDownload);

And add the setUpDownload() function which actually does the copy:

function setUpDownload() {
    // Find the image inside the #qrcode div
    var image = document.getElementById("qrcode").getElementsByTagName("img");

    // Get the src attribute of the image, which is the data-encoded QR code
    var qr = image[0].src;

    // Copy that to the download link
    link.href = qr;
}

And we're done! Working JSFiddle.

Side Note

It is considered best practice to separate your HTML from your JS. That means using event handlers in JS, rather than using inline onclick(). I'd suggest changing this:

<button onclick="generateQR()">Generate</button>

To this (give it an id to make it easy to reference later):

<button id="button">Generate</button>

And adding an event listener in your JS to do the same job:

var button = document.getElementById("button");
button.addEventListener("click", generateQR);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!