<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>
: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: #9b8774ad;
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: #5b92799d;
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;
margin-bottom: 6rem;
}
.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;
}
View Compiled
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.innerText = "Download";
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);
}
}