How to use script in same scripts twice on same page in opencart?

Viewed 43

I am using the following script to show the barcode on the Invoice of my product SKU. But when I use the same script again to show the bar code for the product name{{ product.name }} and SKU barcode disappear, It only shows one barcode ie Product name barcode. How can I use the script to show multiple barcodes, for the product names and SKU?

FYI: I have no knowledge of codes.

<script src="https://cdn.jsdelivr.net/jsbarcode/3.3.20/JsBarcode.all.min.js"></script>
        <h1><img id="upc"/><script> $('#upc').JsBarcode("{{ product.sku }}",{width:1,height:50,lineColor: "#333333" }); </script></p></h1>
1 Answers

First I would say, learn how to use HTML, script and other code! Second (also said already), use correct code.

  1. add the script to your template (only once!), like:

<script src="//cdn.jsdelivr.net/npm/jsbarcode@3.11.5/dist/JsBarcode.all.min.js"></script>

Do that at the end of the template.

  1. use correct HTML-Code, e.g.

<svg class="barcode" jsbarcode-format="code128" jsbarcode-value="{{ item.model }}"></svg>

beside each position the barcode shall be displayed.

  1. call the script and init it:

<script>JsBarcode(".barcode").init();</script>

at the end of your template.

  1. (optional): style your barcode output, according explained here: JSBarcode

p.s.: what I would do, is to store the jsbarcode script locally and call the local stored script.

I know many users intend to use cdn sources. Nice, but not effectful, imagine you want to diplay your solution locally without any internet connection. Or - think about the massive outfall a few weeks ago, where important sources were not reachable.

What you have, is what you can control!

pps: the difference in using a dot instead a # is, # indicates an ID, where the dot is simply a CSS-Class.

Related