Open In App
Related Articles

QR Code Generator using HTML, CSS and jQuery

Improve Article
Improve
Save Article
Save
Like Article
Like

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 get 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>
  <!-- Include Bootstrap for styling -->
  <link rel="stylesheet" href=
 
  <style>
    .qr-code {
      max-width: 200px;
      margin: 10px;
    }
  </style>
 
  <title>QR Code Generator</title>
</head>
 
<body>
  <div class="container-fluid">
    <div class="text-center">
 
      <!-- Get a Placeholder image initially,
       this will change according to the
       data entered later -->
      <img src=
        class="qr-code img-thumbnail img-responsive" />
    </div>
 
    <div class="form-horizontal">
      <div class="form-group">
        <label class="control-label col-sm-2"
          for="content">
          Content:
        </label>
        <div class="col-sm-10">
 
          <!-- Input box to enter the
              required data -->
          <input type="text" size="60"
            maxlength="60" class="form-control"
            id="content" placeholder="Enter content" />
        </div>
      </div>
      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
 
          <!-- Button to generate QR Code for
           the entered data -->
          <button type="button" class=
            "btn btn-default" id="generate">
            Generate
          </button>
        </div>
      </div>
    </div>
  </div>
  <script src=
  </script>
 
  <script>
    // Function to HTML encode the text
    // This creates a new hidden element,
    // inserts the given text into it
    // and outputs it out as HTML
    function htmlEncode(value) {
      return $('<div/>').text(value)
        .html();
    }
 
    $(function () {
 
      // Specify an onclick function
      // for the generate button
      $('#generate').click(function () {
 
        // Generate the link that would be
        // used to generate the QR Code
        // with the given data
        let finalURL =
          htmlEncode($('#content').val()) +
          '&chs=160x160&chld=L|0'
 
        // Replace the src of the image with
        // the QR code image
        $('.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.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 27 Feb, 2023
Like Article
Save Article
Similar Reads
Related Tutorials