JavaScript Barcode Generator Code with Example

JavaScript Barcode Generator Code with Example
Code Snippet:BarCode Generator ♦
Author: Yash Shende
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 1,222
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code snippet helps you to create a barcode generator tool for your web/app project. It uses the JsBarcode library to help you generate barcodes quickly and easily. Whether you’re managing inventory, labeling products, or doing something else entirely, this code streamlines the process and adds value to your project.

You can implement this Barcode Generator code in various web applications and projects where you need to generate barcodes dynamically based on user input. Furthermore, you can customize it to match your project’s design and specific barcode formatting needs.

How to Create Javascript Barcode Generator Code With Example

1. In your HTML file’s <head> section, include Bootstrap CSS, jQuery, Popper JS, and JsBarcode. These resources are essential for the barcode generator to work correctly. Here’s how to add them:

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
  <script src="https://cdn.jsdelivr.net/jsbarcode/3.6.0/JsBarcode.all.min.js"></script>
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.1/css/all.css" integrity="sha384-gfdkjb5BdAXd+lj+gudLWI+BXq4IuLW5IT+brZEZsLFm++aCMlF1V92rMkPaX4PP" crossorigin="anonymous">

2. Create the HTML structure for your barcode generator form. In your HTML body, design the form where users can input the text for which they want to generate a barcode. Here’s a simple example:

 <nav class="navbar navbar-expand-md bg-dark navbar-dark">
  <a class="navbar-brand" href="#">Barc <i class="fab fa-osi"></i>de</a>
<!--   <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="collapsibleNavbar">
    <ul class="navbar-nav">
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Contact Me</a>
      </li>    
    </ul>
  </div>   -->
</nav>
  
  <div class="form-group w-75 mx-auto border py-4 px-2 rounded m-2 ">
      <label for="usr">Enter Text :</label>
      <input type="text" class="form-control" id="usr" name="username">
      <a href="#" class="btn btn-danger btn-lg mt-2" id="btn" onclick="go();">Create barcode</a>
  </div>
  
  
  <div class="mx-auto w-75 p-4 text-center">
<!--     <svg id="barcode" ></svg> -->
    
    <canvas id="barcode" download >

    </canvas>
    
    <br>
    <a href="" class="btn btn-danger btn-lg mt-2" id="btn" download="Barcode.jpg" onclick="downloadBarCode(this);">Download barcode</a>
  </div>
  
  
  
  
  </div>

3. Customize the CSS to style your barcode generator form as needed. You can modify the existing Bootstrap classes or add your own styles to match your project’s design.

canvas{
  width:100%;
  
}
.cd__main{
   display: block !important;
}

4. Finally, initialize the barcode generator by adding the JavaScript functions.

/*document.getElementById("btn").addEventListner("click",function (){
var value = document.getElementById("usr").value;
JsBarcode("#barcode", value);
  
  });*/

function go(){
  
  var value = document.getElementById("usr").value;
  JsBarcode("#barcode", value);
}

downloadBarCode =function (el){
  var canvas = document.getElementById("barcode");
  var image = canvas.toDataURL("image/jpg");
  el.href = image;
};

That’s all! hopefully, you have successfully created a Barcode Generator on your website/app. 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