How to Create a QR Code Generator in JavaScript: Easy Tutorial

How to Create a QR Code Generator in JavaScript: Easy Tutorial


3 min read


QR codes have become an integral part of modern technology, enabling quick access to information with a simple scan. Whether you're a developer looking to integrate QR codes into your projects or just curious about how they work, this guide will walk you through building a QR code generator using JavaScript.

Why QR Codes?

QR codes (Quick Response codes) are two-dimensional barcodes that can store various types of data, such as URLs, contact information, and text. They are widely used in marketing, payments, authentication, and much more.

Getting Started

For this project, we'll use a popular JavaScript library called qrcode.js. This library simplifies the process of generating QR codes in the browser.

Project Setup

First, let's set up our project directory with the following structure:

├── index.html
├── script.js
├── style.css
└── qrcode.min.js

Step 1: Create the HTML File

The HTML file will contain a simple form for inputting the text you want to convert into a QR code and a canvas where the QR code will be displayed.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>QR Code Generator</title>
    <link rel="stylesheet" href="style.css">
    <div class="container">
        <h1>QR Code Generator</h1>
        <form id="qr-form">
            <input type="text" id="qr-input" placeholder="Enter text to generate QR Code" required>
            <button type="submit">Generate QR Code</button>
        <div id="qr-result">
            <div id="qr-canvas"></div>
    <script src="qrcode.min.js"></script>
    <script src="script.js"></script>

Step 2: Style with CSS

To make our application visually appealing, we'll add some basic styles.

body {
    font-family: Arial, sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    background-color: #f0f0f0;

.container {
    text-align: center;
    background: #fff;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);

#qr-form {
    margin-bottom: 20px;

#qr-input {
    padding: 10px;
    width: calc(100% - 24px);
    margin-bottom: 10px;

button {
    padding: 10px 20px;
    border: none;
    background-color: #007bff;
    color: white;
    cursor: pointer;
    border-radius: 4px;

button:hover {
    background-color: #0056b3;

#qr-result {
    margin-top: 20px;

Step 3: Add JavaScript for Functionality

The core functionality of our QR code generator will be handled by the script.js file. We'll use the qrcode.js library to generate the QR code based on the input text.

First, download the qrcode.min.js file from the qrcode.js GitHub repository and place it in your project directory.

Next, add the following JavaScript code to script.js:

document.getElementById('qr-form').addEventListener('submit', function(event) {

    const input = document.getElementById('qr-input').value;
    const qrCanvas = document.getElementById('qr-canvas');

    // Clear any previous QR Code
    qrCanvas.innerHTML = '';

    // Generate new QR Code
    if (input) {
        new QRCode(qrCanvas, {
            text: input,
            width: 256,
            height: 256

Testing the Application

Open the index.html file in your browser. You should see a form where you can input text to generate a QR code. Upon submitting the form, the QR code will be displayed below the input field.


Congratulations! You've successfully created a QR code generator using JavaScript. This project demonstrates how easy it is to integrate QR code functionality into your web applications. Whether for personal projects or professional use, understanding how to generate and utilize QR codes can be a valuable skill.

Feel free to expand this project by adding features such as downloading the QR code image, customizing the QR code's appearance, or integrating it with a backend service. The possibilities are endless!

Happy coding!

💰 You can help me by Donating