QR Code Generator using HTML, CSS & JavaScript – Coding Torque

Share your love

Hello Guys! Welcome to Coding Torque. In this blog, I’m going to explain to you how to make a QR Code Generator using HTML, CSS, and JavaScript. This will be a step-by-step guide. Let’s get started 🚀.

Before we start, here are some JavaScript Games you might like to create:

1. Snake Game using JavaScript

2. 2D Bouncing Ball Game using JavaScript

3. Rock Paper Scissor Game using JavaScript

4. Tic Tac Toe Game using JavaScript

5. Whack a Mole Game using JavaScript

 

I would recommend you don’t just copy and paste the code, just look at the code and type by understanding it.

 

HTML Code 

Starter Template

<!DOCTYPE html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Font Awesome Icons  -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"
        integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA=="
        crossorigin="anonymous" />


    <title>QR Code Generator using JavaScript - @code.scientist x @codingtorque</title>

</head>

<body>

    <script src="https://cdn.rawgit.com/davidshimjs/qrcodejs/gh-pages/qrcode.min.js"></script>
</body>

</html>   

Paste the below code in your <body> tag

<form>
    <h2 class="heading">QR Code Generator</h2>

    <input type="url" id="website" name="website" placeholder="https://codingtorque.com" required />

    <div id="qrcode-container">
        <div id="qrcode" class="qrcode"></div>
    </div>

    <button type="button" class="generateBtn" onclick="generateQRCode()">Generate QR Code</button>
</form>

Output Till Now

CSS Code 

Create a file style.css and paste the code below.

body {
    display: flex;
    font-family: poppins;
    justify-content: center;
    align-items: center;
    background-color: black;
    color: white;
    height: 600px;
}

form {
    height: 30rem;
    width: 20rem;
    background-color: white;
    color: black;
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
    overflow: hidden;
}

.heading {
    text-align: center;
    background: deepskyblue;
    color: white;
    margin: 0;
    margin-bottom: 20px;
    padding: 10px 0;
    width: 100%;
}

#website {
    width: 80%;
    height: 30px;
    outline: none;
    border: none;
    background: gainsboro;
    padding: 0 10px;
    margin-bottom: 20px;
}

input {
    font-family: poppins;
    transition: 0.3s;
    border-radius: 5px;
    border: none;
    outline: none;
}

.generateBtn {
    padding: 10px 20px;
    border-radius: 10px;
    border: none;
    outline: none;
    background-color: black;
    color: white;
    cursor: pointer;
    margin: 20px;
}

Output Till Now

qr code generator using javascript with source code

JavaScript Code 

Create a file script.js and paste the code below.
function generateQRCode() {
    let website = document.getElementById("website").value;
    if (website) {
        let qrcodeContainer = document.getElementById("qrcode");
        qrcodeContainer.innerHTML = "";
        new QRCode(qrcodeContainer, website);

        document.getElementById("qrcode-container").style.display = "block";
    } else {
        alert("Please enter a valid URL");
    }
}
Written by: Piyush Patil
If you have any doubts or any project ideas feel free to Contact Us
Hope you find this post helpful💖
Share your love