Igorski88
Igorski88

Reputation: 1173

Cant get a bootstrap tooltip to display an image created in JS

I am trying to display a barcode image that is generated via JsBarcode inside a bootstrap tooltip.

What I have tried:

  1. I tried to move around the Tooltip js code in front and behind the JsBarcode js code.

  2. I have searched for anything similar online but the only thing that somewhat resembles a solution is to trigger the JsBarcode code on a btn click and the bootstrap tooltip on page load. This doesn't work for me. I was looking for both to be initialized on page load so that the tooltip would work.

HTML:

    <a id="SellerSKU_ToolTip" data-toggle="tooltip" data-html="true" data-placement="bottom" class="White-tooltip" title="<svg id='Barcode_Seller_SKU'></svg>">123456789012</a></td>

JS Code:

   $(document).ready(function () {

    $(function () {
        $('[data-toggle="tooltip"]').tooltip()
    })

    JsBarcode("#Barcode_Seller_SKU", "1234", {
        format: "auto",
        lineColor: "#0aa",
        width: 2,
        height: 25,
        displayValue: true
    });
    }

The error:

JsBarcode.all.min.js:3 Uncaught TypeError: r.options(...)[n.format] is not a function
    at j (JsBarcode.all.min.js:3)
    at HTMLDocument.eval (eval at globalEval (jquery.min.js:2), <anonymous>:11:9)
    at j (jquery.min.js:2)
    at Object.add [as done] (jquery.min.js:2)
    at n.fn.init.n.fn.ready (jquery.min.js:2)
    at eval (eval at globalEval (jquery.min.js:2), <anonymous>:2:17)
    at eval (<anonymous>)
    at Function.globalEval (jquery.min.js:2)
    at n.fn.init.domManip (jquery.min.js:3)
    at n.fn.init.append (jquery.min.js:3)
j @ JsBarcode.all.min.js:3
(anonymous) @ VM200:11

I would be grateful for any help or advice.

Upvotes: 0

Views: 150

Answers (1)

a_nain
a_nain

Reputation: 185

The error here is because your code:

JsBarcode("#Barcode_Seller_SKU", "1234", {

is expecting the SVG element to be present on document ready.

But since the tooltip content is generated dynamically on-hover of the element, the SVG element you're expecting to make into a barcode would only be available after you hover over the anchor element.

You can use Bootstrap's tooltip on-show event to create your barcode. Something like this:

$(function() {
    var tt = $('[data-toggle="tooltip"]').tooltip({
        placement: "right"
    });
    
    tt.on("shown.bs.tooltip", function() {
    	JsBarcode("#Barcode_Seller_SKU", "123456789012", {
            format: "EAN13",
            lineColor: "#0aa",
            width: 2,
            height: 25,
            displayValue: true
        });
    });
});
.tooltip-inner { max-width: none !important; }
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/jsbarcode/3.6.0/JsBarcode.all.min.js"></script>

<a id="SellerSKU_ToolTip" data-toggle="tooltip" data-html="true" data-placement="bottom" class="White-tooltip" title="<svg id='Barcode_Seller_SKU'></svg>">123456789012</a>

Upvotes: 2

Related Questions