Hello friends, today I will tell you through experts php tutorial how you can create QR Code generator using JavaScript code. So let’s try to understand step to step with example.
Step 1:- First of all copy html code and past your file.
qrcode.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> </head> <body> <div class="user-input-section"> <section class="heading"> <div class="title">QRcodes</div> <div class="sub-title">Generate QRCode for anything!</div> </section> <section class="user-input"> <input type="text" placeholder="Type something..." name="input_text" id="input_text" autocomplete="off"> <button class="button" type="submit">Generate<i class="fa-solid fa-rotate"></i></button> </section> </div> <div class="qr-code-container"> <div class="qr-code" style></div> </div> </body> </html>
CSS Code Create
$primary-color: #2b303a; $secondary-color: #eee5e9; $tertiary-color: #7c7c7c; $qr-code: #7c7c7c33; $button-color: #92dce5; :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; align-items: center; justify-content: center; background-color: #eae6e5; background-color: $primary-color; height: 100vh; @media screen and (max-width: 50em) { flex-direction: column; height: 100%; } .heading { margin: 3rem 0 5rem 0; } .title, .sub-title { font-size: 4rem; text-align: center; font-family: "Poppins", sans-serif; color: #12130f; color: $secondary-color; } .sub-title { font-size: 1.5rem; color: $secondary-color; opacity: 0.5; } .user-input-section { display: flex; flex-direction: column; align-items: center; width: 40%; @media screen and (max-width: 50em) { width: 100%; margin: 2rem 0 0 0; } .user-input { display: flex; flex-direction: column; align-items: center; width: 100%; label { text-align: center; font-size: 1.5rem; font-family: "Poppins", sans-serif; } input { width: 80%; max-width: 35rem; font-family: "Poppins", sans-serif; outline: none; border: none; border-radius: 0.5rem; background-color: #9b8774ad; background-color: $tertiary-color; text-align: center; padding: 1.5rem 1rem; margin: 1rem 1rem 2rem 1rem; color: black; &::placeholder { color: $secondary-color; color: $primary-color; } } } } .button { outline: none; border: none; border-radius: 0.5rem; padding: 1.5rem 2.5rem; margin-bottom: 3rem; background-color: #5b92799d; background-color: $button-color; color: black; font-family: "Nunito", sans-serif; font-size: 1.6rem; i { margin-left: 1rem; } } .qr-code-container { display: flex; justify-content: center; align-items: center; width: 40%; @media screen and (max-width: 50em) { width: 100%; margin: 8rem 0; } .qr-code { display: flex; border-radius: 1rem; background-color: $qr-code; width: fit-content; flex-direction: column; justify-content: space-between; padding: 2rem; button { display: flex; justify-content: center; background-color: #1f1f1f; color: #eae6e5; border: none; outline: none; width: 100%; margin-top: 2.5rem; border-radius: 1rem; a { font-family: "Poppins", sans-serif; font-size: 1.5rem; width: 100%; height: 100%; text-decoration: none; color: #eae6e5; padding: 1rem; i { margin-left: 0.5rem; } } } } } }
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); } }