Skip to content
Related Articles

Related Articles

Barcode Code Generator using jQuery Plugin

View Discussion
Improve Article
Save Article
  • Last Updated : 26 Jun, 2022

In this article, we are going to generate the barcode for any text value using HTML, CSS, and Jquery.

Barcode is the unique representation of the characters and numbers in the form of lines and space. It is widely used in stores and many places. Current day, you have seen the barcode on most of the products that are sold outs in the market. We can just scan the barcode using the barcode reader to get the information about that product. It makes our work easy to get the product information. Also, there are a lot of other uses for the barcode.


Types of barcode: There are various types of barcodes available to use. All types of barcodes have different applications and different specifications. Users can use any barcode type according to their requirements.  

  • EAN 8
  • EAN 13
  • Data Matrix (2D barcode)
  • UPC
  • code 11
  • code 39
  • code 93
  • code 128
  • codabar
  • standard 2 of 5 (industrial)
  • interleaved 2 of 5
  • MSI

Prerequisite: Users need to download the below jquery-barcode.js and jquery-barcode.min.js library and need to add them between the <head> section as shown below.

<script type="text/javascript" src="jquery-barcode.js"></script>
<script type="text/javascript" src="jquery-barcode.min.js"></script>

Syntax: Here, we will use the barcode() method from the JQuery barcode.js library. Users can follow the syntax of the barcode method as per below.

// Initialize input variable
let inputValue = "1234567865"; 

// initialize barcode type variable
let barcodeType = "ean8";

// initialize settings for the barcode
var settings = {
        output: "canvas", // renderer type
        bgColor: '#FFFFFF', //background color
        color: '#000000', // barcode color
        barWidth: '1.5', // canvas width
        barHeight: '70', // canvas height
        moduleSize: '5',
        posX: '15', // starting x position in canvas
        posY: '30', // starting y position in canvas
        addQuietZone: '1'

$('#id_of_output_div').barcode(inputValue, barcodeType, settings);

Example: In the below example, we have written the HTML, CSS, and Jquery code to generate the barcode. Users can create a single HTML file and add the below code to that to test the code.


    <title>Barcode Generator</title>
        body {
            max-width: 100%;
            text-align: center;
        .mainDiv {
            background: green;
            font-family: Arial;
            padding: 25px;
            max-height: 73s0px;
            width: 300px;
            text-align: justify;
            display: flex;
            flex-direction: column;
            margin: 20px auto;
        .mainDiv .row {
            margin-bottom: 20px;
            overflow: hidden;
        label {
            margin: 5px;
            color: lightgrey;
        h2 {
            margin-bottom: 10px;
            color: white
        .input_box {
            padding: 10px;
            border: none;
            background-color: white;
            width: 100%;
            margin-top: 5px;
        .button {
            background-color: grey;
            padding: 10px 40px;
            color: white;
            border: none;
    <script src=
    <script type="text/javascript" 
    <script type="text/javascript" 
    <h1>Barcode Generator using JQuery</h1>
    <div class="mainDiv">
        <div class="row">
            <label>Type The Text</label>
            <br />
            <input type="text" id="textValue"
                   value="92312342432" class="input_box">
        <div class="row">
            <!-- Different types of barcode -->
                <h2>Choose Barcode Type:</h2>
                <input type="radio" name="barcodeType" 
                       value="ean8" checked="checked">
                <label>EAN 8</label>
                <br />
                <input type="radio" name="barcodeType" 
                <label>EAN 13</label>
                <br />
                <input type="radio" name="barcodeType"
                <label>Data Matrix (2D barcode)</label>
                <br />
                <input type="radio" name="barcodeType"
                <br />
                <input type="radio" name="barcodeType"
                <label>code 11</label>
                <br />
                <input type="radio" name="barcodeType"
                <label>code 39</label>
                <br />
                <input type="radio" name="barcodeType" 
                <label>code 93</label>
                <br />
                <input type="radio" name="barcodeType" 
                <label>code 128</label>
                <br />
                <input type="radio" name="barcodeType" 
                <br />
                <input type="radio" name="barcodeType"
                <label>standard 2 of 5 (industrial)</label>
                <br />
                <input type="radio" name="barcodeType" 
                <label>interleaved 2 of 5</label>
                <br />
                <input type="radio" name="barcodeType" 
                <br />
            <!-- Different renderer types of the barcode -->
                <h2>Choose Barcode Format</h2>
                <input type="radio" name="rendererType"
                       value="css" checked="checked">
                <br />
                <input type="radio" name="rendererType" 
                <br />
                <input type="radio" name="rendererType"
                <br />
                <input type="radio" name="rendererType" 
                <br />
        <div class="row">
            <input type="button" onclick="generateBarcode();"
                   value="Generate the barcode" class="button">
        <div class="row">
            <div id="barcodeoutput"></div>
            <canvas id="canvasOutput" width="200" 
    <script type="text/javascript">
        //  Function to generate the barcode
        function generateBarcode() {
            // Taking input values from the user
            // Text value
            var inputValue = $("#textValue").val(); 
            // Barcode type
            var barcodeType = $("input[name=barcodeType]:checked").val(); 
            // Renederer type
            var rendererType = $("input[name=rendererType]:checked").val(); 
            // Settings to generate barcode
            var settings = {
                output: rendererType,
                bgColor: '#FFFFFF',
                color: '#000000',
                barWidth: '1.5',
                barHeight: '70',
                moduleSize: '5',
                posX: '15',
                posY: '30',
                addQuietZone: '1'
            if (rendererType != 'canvas') {
                // If renderer type is not canvas, show barcodeoutput div and 
                // add output from barcode() function to that div
            } else {
                // If renderer type is canvas, create new canvas by 
                // clearing previous one, and add the output generated 
                // from barcode() function to new canvas
        // Function to clear canvas.
        function createCanvas() {
            // Get canvas element from HTML
            var canvas = $('#canvasOutput').get(0);
            // Add 2d context to canvas
            var ctx = canvas.getContext('2d');
            // Clear canvas
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            ctx.strokeRect(0, 0, canvas.width, canvas.height);

Output: In the output, users can see how we can generate various types of barcodes and how we can use the various types of renderers.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!