Pure JavaScript QR Code Generator

Pure JavaScript QR Code Generator
Code Snippet:QR Code Generator using JavaScript
Author: Max Programming
Published: January 9, 2024
Last Updated: January 22, 2024
Downloads: 997
License: MIT
Edit Code online: View on CodePen
Read More

This “Pure JavaScript QR Code Generator” code allows you to generate QR codes based on the text input you provide. It uses a JavaScript library to create QR codes dynamically as you enter text. This tool is helpful for quickly generating QR codes for various purposes.

You can use this code on your website to generate QR codes for links, text, or data. It’s beneficial for easily creating QR codes for sharing URLs, contact information, or access points.

How to Create a Pure Javascript QR Code Generator

1. To begin, make sure you have the following HTML elements in your webpage:

<h1>QR Code Generator</h1>
<input type="text" spellcheck="false" id="text" value="https://google.com" />
<div id="qrcode"></div>

2. You can customize the appearance of the QR code by modifying the CSS. Feel free to adjust styles such as the input field’s design or the QR code’s size.

* {
	margin: 0;
	padding: 0;
	box-sizing: border-box;
}

body {
	width: 80%;
	height: 100vh;
	margin: auto;
	display: grid;
	place-items: center;
}

h1 {
	font-family: sans-serif;
}

input {
	padding: 10px;
	border-radius: 20px;
	border: 2px solid steelblue;
	font-size: 1.5rem;
	letter-spacing: 2px;
	outline: none;
}

3. Now, load the qrcode JS by adding the following CDN link before closing the body tag:

<script src='https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js'></script>

4. Finally, add the following JavaScript code between <script> tag (or external JS file) in your webpage. This code initializes the QR code generator and handles user input.

const qrcode = document.getElementById("qrcode");
const textInput = document.getElementById("text");

const qr = new QRCode(qrcode);

textInput.oninput = (e) => {
	qr.makeCode(e.target.value.trim());
};

qr.makeCode(textInput.value.trim());

That’s all! hopefully, you have successfully created a QR code Generator for your web/app project. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X