How to Make A QR Code Generator in JavaScript

We use QR codes at many places in our daily life and it means “Quick Response” which means we can access anything quickly with the help of a QR code and for that we just have to scan the provided QR code. That is why in this article I will teach you step by step how to create a QR Code Generator In JavaScript.

In this project, along with generating the QR code, I will also teach you to download the QR code, and how you can download the generated QR code.

How to Make A QR Code Generator in JavaScript

Algorithm Of QR Code Generator In JavaScript

Before making the QR Code Generator project, it is very important for us to understand its algorithm because we are going to make this project based on the algorithm.

Here is the algorithm:

  • Getting Information:
    • First of all, we have to take data from the user for which the user needs a QR code.
  • Checking the Input:
    • We need to ensure that users cannot enter empty text
  • Making a QR Code:
    • We have to create a QR code based on the user data/information provided.
  • Showing the QR Code:
    • Show the QR code on the webpage.
  • Download Option:
    • Give the QR code image a download button to make it downloadable for users.

This was the algorithm of the QR Code Generator, Now we have to create our QR Code Generator project with the help of this algorithm and convert the algorithm into code and for which we are going to use HTML CSS and JavaScript

Prerequisites To Make QR Code Generator Using JavaScript

To create a QR Code Generator project with the help of HTML, CSS, and JavaScript, You should have basic knowledge of HTML5, CSS, and JavaScript.

Although this is a beginner-friendly project but you still need to know the basics to understand the code.

Here is Prerequisites:

  • Basic Understanding of HTML:
    • Understanding of HTML (Hypertext Markup Language) is used for structuring content on webpages.
  • Basic Understanding of CSS:
    • Basic knowledge of CSS (Cascading Style Sheets) for styling our webpage
  • Basic Understanding of JavaScript:
    • It is important to know the basics of JavaScript to make our project interactive.
  • Code Editor:
    • A Code editor, such as Visual Studio Code or Sublime Text, for writing and editing your HTML, CSS, and JavaScript code.

QR Code Generator HTML Code

Here is the HTML code for the QR Code Generator project using HTML CSS and JavaScript. This is a simple HTML code for the structure of our webpage which you can easily understand if you have basic knowledge of HTML.

You can copy the entire code using the “Copy” button to use the code directly in your project, just paste the code into your HTML file.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>QR Code Generator</title>

    <!-- Link to an external stylesheet (style.css) for better code organization -->
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <div id="qr-container">
        <h1>QR Code Generator</h1>

        <!-- Input field where the user can enter data for the QR code -->
        <input type="text" id="qr-input" placeholder="Enter data">
        
        <!-- Button that triggers the generation of the QR code -->
        <button onclick="generateQRCode()">Generate QR Code</button>

        <!-- Container to display the generated QR code -->
        <div id="qr-code"></div>

        <!-- Download link for the generated QR code as a PNG image -->
        <a id="download-link" download="qrcode.png">Download QR Code</a>
    </div>
</body>

<!-- Include the qrcodeJS generation library hosted on a CDN -->
<script src="https://cdn.rawgit.com/davidshimjs/qrcodejs/gh-pages/qrcode.min.js"></script>
<!-- Include a separate JavaScript file (script.js) for better code organization -->
<script src="script.js"></script>

</html>
HTML

QR Code Generator CSS Code

Here is the CSS code with a step-by-step guide using comments in the CSS code and you can also copy the entire code using the “Copy” button on the right top corner.

CSS
/* Overall webpage styling */
body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    background: linear-gradient(to right, #3494e6, #ec6ead);
}

/* Styling for the QR Code Generator container */
#qr-container {
    background-color: #ffffff;
    border-radius: 10px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    padding: 20px;
    text-align: center;
    color: #333;
}

/* Main heading styling */
h1 {
    color: #2c3e50;
}

/* Styling for the QR code image container */
#qr-code {
    margin-top: 20px;
    padding: 10px;
}

/* Styling for the QR code image */
#qr-code img {
    padding: 10px;
    border: 2px solid #000;
    display: inline-block;
    margin: auto;
}

/* Styling for the input field */
input[type="text"] {
    width: 100%;
    padding: 10px;
    box-sizing: border-box;
    margin-bottom: 15px;
}

/* Styling for buttons */
button, a {
    background: linear-gradient(to right, #3494e6, #ec6ead);
    color: #ffffff;
    padding: 10px 20px;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    transition: 0.3s;
}

/* Hover effect for buttons */
button:hover, a:hover {
    background: linear-gradient(to right, #ec6ead, #3494e6);
    box-shadow: 0 0 10px #adadad;
}

/* Styling for the download link */
a {
    display: none;
    margin-top: 10px;
    text-decoration: none;
    font-weight: bold;
    width: fit-content;
    margin-inline: auto;
}
CSS

QR Code Generator JavasScript Code

Here is our JavaScript code for the QR Code Generator using JavaScript. You can easily understand the code because I have added comments at each step.

You can also copy the whole code and paste it into your project’s javascript file.

JavaScript
function generateQRCode() {
    // Get user input
    const inputData = document.getElementById('qr-input').value;

    // Check if input is empty, if empty then give a alert message
    if (inputData.trim() === '') {
        alert('Please enter data');
        return;
    }

    // Get QR code container element and remove previous QR code
    const qrCodeContainer = document.getElementById('qr-code');
    qrCodeContainer.innerHTML = '';

    // Generate new QR code with "QRCode" function from the "qrcodejs" library
    const qr = new QRCode(qrCodeContainer, {
        text: inputData,
        width: 150,
        height: 150,
    });

    // Create QR Code download link and display the download button
    const downloadLink = document.getElementById('download-link');
    downloadLink.href = qrCodeContainer.querySelector('canvas').toDataURL();
    downloadLink.style.display = 'block';
}
JavaScript

Here is the explanation of QR Code Generator’s JavaScript code:

  • Getting Data From User:
    • The code starts by taking whatever the user types into a box on the webpage.
  • Checking the Input:
    • Checking if the user left that box empty using a conditional statement. If they did, it gives an alert message.
  • Making a QR Code:
    • If the user types something in the Input box, the “new QRCode” function makes a new QR code with the typed information.
  • Showing the QR Code:
    • Then show the newly generated QR code on the webpage.
  • Download Option:
    • At the end generate a downloadable link for the QR code so a user can download it into their device.

This is how you can make a QR code generator using JavaScript.

Read this: Dark Mode Toggle using HTML CSS and JavaScript

Read this: How To Create Digital Clock Using HTML CSS and Javascript

Read this: Image Slider using HTML CSS and JavaScript

Read this: Make A Random Quote Generator Using HTML, CSS, and JavaScript

Read this: ToDo List App in JavaScript for Beginners

Conclusion

So in this article, we learned how we can make a QR Code Generator Project using HTML CSS, and JavaScript.

I tried to make you understand the code step by step in the entire article so that you can create your own QR Code Generator project and learn JavaScript better.

Still, if you are facing any error or bug in your project, then you can comment with the problem statement in the comment box given below, we will try to find the solution to the problem and reply to you as soon as possible.

Thank you for reading this article completely and to know JavaScript better, you can also read our other blog posts and project tutorials.

Leave a comment