John Davidson

php - JsBarcode options does not work with jQuery


I'm using jQuery to generate the barcodes and tried to add the size options but it doesn't work. The demo at isn't working for me because I am using php to loop through the items then only loop it again to generate the barcode.

<!DOCTYPE html>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" href="">
<script src=""></script>
<script src="">

<div class="container bg-white">
<?php if(!empty($items)){ foreach($items as $row){ ?>
<svg id="<?= $row['sn']; ?>"></svg>
<?php }} ?>

const items = <?= !empty($items) ? json_encode($items) : json_encode([]); ?>;
if(items){ => {
$(`#${}`).JsBarcode(`SSN : ${}`)
.options({font: "OCR-B", height: 25, width: 35})

<script src=""></script>
<script src=""></script>

My Answer:
To use JsBarcode options with jQuery, you can pass the options as an object when calling the JsBarcode function. Here's an example of how you can do this:

// Loop through items and generate barcodes
$('.item').each(function() {
var barcodeValue = $(this).data('barcode');

// Generate barcode with options
JsBarcode(this, barcodeValue, {
format: "CODE128",
width: 2,
height: 50,
displayValue: false

In this example, we are looping through elements with the class 'item', getting the barcode value from a data attribute, and then calling JsBarcode with the specified options (format, width, height, displayValue).

Make sure to include the JsBarcode library in your project and also include jQuery before the script that contains the above code.

If you are still facing issues, please provide more details or code snippets so that I can assist you further.

Rate this post

4 of 5 based on 7953 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us