QR Code Generator Using HTML, CSS and Jquery
QR Code Generator Using HTML, CSS and Jquery
jQuery
https://www.geeksforgeeks.org/qr-code-generator-using-html-css-and-jquery/
A QR code generator is an application that stores any required textual data into a QR code
which can be later scanned with a QR code scanner to reveal the stored information. This QR
Code can be used anywhere, for example, on a poster or website to allow users to get additional
information. This application will allow the user to type in the data required and save it a PNG
or SVG image of the QR code.
Approach: To generate the QR code, we will use the Google Charts API. Using jQuery, the
QR code image to be displayed is updated according to the image returned by the API.
The API endpoint that would be used is given below.
https://chart.googleapis.com/chart?chs=150×150&cht=qr&chl=Hello%20world
Explanation of the URL:
The root URL for Google Chart Infographics is https://chart.googleapis.com/chart. This
can be specified with the required parameters to ger the desired output.
The chs parameter denotes the size of the QR code image in pixels.
The cht parameter denotes the type of the image to be created. The value “qr” will be used
to generate a QR Code.
The chl parameter denotes the text or URL data to be encoded in the QR code.
Example:
HTML
<!DOCTYPE html>
<html>
<head>
"https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
<style>
.qr-code {
max-width: 200px;
margin: 10px;
</style>
</head>
<body>
<div class="container-fluid">
<div class="text-center">
<img src=
"https://chart.googleapis.com/chart?cht=qr&chl=Hello+World&chs=160x160&chld=L|0"
<div class="form-horizontal">
<div class="form-group">
for="content">
Content:
</label>
<div class="col-sm-10">
maxlength="60" class="form-control"
</div>
</div>
<div class="form-group">
Generate
</button>
</div>
</div>
</div>
</div>
<script src=
"https://code.jquery.com/jquery-3.5.1.js">
</script>
<script>
function htmlEncode(value) {
return $('<div/>').text(value)
.html();
}
$(function () {
$('#generate').click(function () {
let finalURL =
'https://chart.googleapis.com/chart?cht=qr&chl=' +
htmlEncode($('#content').val()) +
'&chs=160x160&chld=L|0'
$('.qr-code').attr('src', finalURL);
});
});
</script>
</body>
</html>
Output:
jQuery is an open source JavaScript library that simplifies the interactions between an
HTML/CSS document, It is widely famous with it’s philosophy of “Write less, do more”.
You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery
Examples.