ADVERTISEMENT

QR Code Generator using Vanilla JavaScript

Telegram Group Join Now

ADVERTISEMENT

QR Code Generator using Vanilla JavaScript

QR Code Generator using Vanilla JavaScript
QR Code Generator using Vanilla JavaScript

 

ADVERTISEMENT

ADVERTISEMENT

Welcome to the Codewithrandom blog. In this blog, We learn how to create a QR Code Generator using Vanilla JavaScript. We use Html, Css, and JavaScript for this QR Code Generator.

I hope you enjoy our blog so let’s start with a basic html structure for a QR Code Generator.

ADVERTISEMENT

HTML Code For QR Code Generator

<body>
<section class="heading">
<div class="title">QRcodes</div>
<div class="sub-title">Generate QRCode for anything!</div>
</section>
<section class="user-input">
<label for="input_text">Type something...</label>
<input type="text" name="input_text" id="input_text" autocomplete="off">
<button class="button" type="submit">Generate QR Code</button>
</section>
<div class="qr-code" style="display: none;"></div>
</body>

There Is All The Html Code For The Qr Code Generator. Now, You Can See Output Without Css And Javascript. Then We Write Css Code For Styling & Qr Code Functionality using Javascript Code.

ADVERTISEMENT

Portfolio Website using HTML and CSS (Source Code)

Output

QR Code Generator using Vanilla JavaScript
QR Code Generator using Vanilla JavaScript

CSS Code For QR Code Generator

:root {
font-size: 62.5%;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
text-size-adjust: none;
-webkit-text-size-adjust: none;
}
button:hover {
cursor: pointer;
}
body {
display: flex;
flex-direction: column;
align-items: center;
background-color: #eae6e5;
}
.heading {
margin: 3rem 0 5rem 0;
}
.title, .sub-title {
font-size: 4rem;
text-align: center;
font-family: "Poppins", sans-serif;
color: #12130f;
}
.sub-title {
font-size: 1.5rem;
color: #8f8073;
}
.user-input {
display: flex;
flex-direction: column;
align-items: center;
width: 100%;
}
.user-input label {
text-align: center;
font-size: 1.5rem;
font-family: "Poppins", sans-serif;
}
.user-input input {
width: 80%;
max-width: 35rem;
font-family: "Poppins", sans-serif;
outline: none;
border: none;
border-radius: 0.5rem;
background-color: #9b8774 ad;
text-align: center;
padding: 0.7rem 1rem;
margin: 1rem 1rem 2rem 1rem;
}
.button {
outline: none;
border: none;
border-radius: 0.5rem;
padding: 0.7rem 1rem;
margin-bottom: 3rem;
background-color: #5b9279 9d;
color: #12130f;
font-family: "Poppins", sans-serif;
}
.qr-code {
border-top: 0.5rem solid #8f8073;
border-right: 0.5rem solid #8f8073;
border-bottom: 1rem solid #8f8073;
border-radius: 0.5rem;
border-bottom-left-radius: 0.5rem;
border-bottom-right-radius: 0.5rem;
border-left: 0.5rem solid #8f8073;
background-color: #8f8073;
}
.qr-code button {
display: flex;
justify-content: center;
background-color: #8f8073;
font-family: "Poppins", sans-serif;
color: #eae6e5;
border: none;
outline: none;
width: 100%;
height: 100%;
margin-top: 1rem;
}
.qr-code button a {
width: 100%;
height: 100%;
text-decoration: none;
color: #eae6e5;
}

Ecommerce Website Using HTML, CSS, & JavaScript (Source Code)

ADVERTISEMENT

We have completed our CSS section,  Here is our updated output HTML + CSS.

Output

QR Code Generator using Vanilla JavaScript
QR Code Generator using Vanilla JavaScript

QR Code Generator using Vanilla JavaScript

use this qrcodejs CDN link in Html File Section using the script tag because this is the main link if you do not link correctly your project never works so add it carefully.

ADVERTISEMENT

100+ JavaScript Projects With Source Code ( Beginners to Advanced)

ADVERTISEMENT

Here is the CDN link 👇

https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js

JavaScript Code

let btn = document.querySelector(".button");
let qr_code_element = document.querySelector(".qr-code");

btn.addEventListener("click", () => {
  let user_input = document.querySelector("#input_text");
  if (user_input.value != "") {
    if (qr_code_element.childElementCount == 0) {
      generate(user_input);
    } else {
      qr_code_element.innerHTML = "";
      generate(user_input);
    }
  } else {
    console.log("not valid input");
    qr_code_element.style = "display: none";
  }
});

function generate(user_input) {
  qr_code_element.style = "";

  var qrcode = new QRCode(qr_code_element, {
    text: `${user_input.value}`,
    width: 180, //128
    height: 180,
    colorDark: "#000000",
    colorLight: "#ffffff",
    correctLevel: QRCode.CorrectLevel.H
  });

  let download = document.createElement("button");
  qr_code_element.appendChild(download);

  let download_link = document.createElement("a");
  download_link.setAttribute("download", "qr_code.png");
  download_link.innerHTML = `Download <i class="fa-solid fa-download"></i>`;

  download.appendChild(download_link);

  let qr_code_img = document.querySelector(".qr-code img");
  let qr_code_canvas = document.querySelector("canvas");

  if (qr_code_img.getAttribute("src") == null) {
    setTimeout(() => {
      download_link.setAttribute("href", `${qr_code_canvas.toDataURL()}`);
    }, 300);
  } else {
    setTimeout(() => {
      download_link.setAttribute("href", `${qr_code_img.getAttribute("src")}`);
    }, 300);
  }
}

generate({
  value: "https://www.codewithrandom.com/"
});

Final Output QR Code Generator using Vanilla JavaScript

QR Code Generator using Vanilla JavaScript
QR Code Generator using Vanilla JavaScript

 

Restaurant Website Using HTML and CSS

ADVERTISEMENT

Now we have completed our QR code generator. Here is our updated output with JavaScript. Hope you like the QR code generator. you can see the output video and project screenshots. See our other blogs and gain knowledge in front-end development.

ADVERTISEMENT

Thank you!

ADVERTISEMENT

In this post, we learn how to create a QR code generator Using HTML, CSS, and JavaScript. If we made a mistake or any confusion, please drop a comment to reply or help you learn easily.

ADVERTISEMENT

Written by – Code With Random/Anki 

ADVERTISEMENT

Code by – Murtuza

ADVERTISEMENT

Telegram Group Join Now

Share on:

0 thoughts on “QR Code Generator using Vanilla JavaScript”

  1. ADVERTISEMENT

Leave a Comment

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT