Generate a QR code in Javascript
Published in
1 min readDec 31, 2022
Here is an example of how you can generate a QR code in JavaScript using the qrcode-generator
library:
// First, install the library using npm:
// npm install qrcode-generator
// Then, import the library in your JavaScript file:
const qrcode = require('qrcode-generator');
// Next, create a new QR code using the create method:
const qr = qrcode(0, 'M');
// Set the size of the QR code using the size method:
qr.addData('https://www.example.com');
qr.make();
// Finally, get the QR code as a DOM element using the createImgTag method:
const qrCodeElement = qr.createImgTag(4);
// You can then append the QR code element to the DOM:
document.body.appendChild(qrCodeElement);
This will generate a QR code with the data https://www.example.com
and add it to the DOM as an image tag. You can customize the size and error correction level of the QR code by passing different values to the qrcode
function.
Note: This is just one example of how you can generate a QR code in JavaScript. There are many other libraries and approaches you can use, depending on your specific requirements.