Search code examples
jquerytippyjs

tippy plugin cannot show multiple tooltips with HTML content


I have a tippy plugin initialized this way:

tippy('.tooltip_commands', {
                    delay: 100,
                    arrow: true,
                    arrowType: 'round',
                    size: 'large',
                    duration: 500,
                    animation: 'scale',
                    placement: 'left',
                    interactive: true,
                    trigger: 'click',
                    content: document.querySelector('#tooltip_content').cloneNode(true)
                });

Where "tooltip_content" is:

<div class="tooltip_templates">
    <div id="tooltip_content">
        <ul class="list-group">
            <li class="list-group-item">
                <a id="_bajarFactura" href="#" title="Bajar factura"><i class="sl-icon-cloud-download"></i>Bajar factura original</a>
            </li>
            <li class="list-group-item">
                <a id="_verFactura" data-toggle="modal" href="#myModal" title="Ver archivo de la factura"><i class="sl-icon-doc"></i>Ver factura original</a>
            </li>
            <li class="list-group-item">
                <a id="_verDetalle" href="#" title="Ver detalles de la factura"><i class="sl-icon-magnifier-add"></i>Ver Detalle</a>
            </li>
            <li class="list-group-item">
                <a id="_reEnviar" href="#" title="Re-enviar factura a un e-mail"><i class="icon-Mail-Forward"></i>Re-enviar</a>
            </li>
        </ul>
    </div>
</div>

And "tooltip_commands" are several buttons in a TABLE (one button for each row).

After I run the page, only the last row shows the tooltip with the HTML content. All other rows shows the tooltip, but empty.

Is there a way to solve it?

Please see this Fiddle with the problem:

https://jsfiddle.net/desytec/ym4nubhw/21/


Solution

  • When you call the tippy function, JavaScript first evaluates your arguments. Most importantly the content property of the options object will get evaluated:

    document.querySelector('#tooltip_content').cloneNode(true)
    

    This expression will evaluate to a single copy of the #tooltip_content element. Tippy then will try to assign this same single copy of your tooltip to every popup.

    In order for your example to work correctly you have to create a tooltip container element for each tooltip you create. I would do this with a jQuery loop, but there are many other solutions as well:

    $(document).ready(function () {
      $('.tooltip_commands').each(function(){
                    tippy(this, {
                        delay: 100,
                        arrow: true,
                        arrowType: 'round',
                        size: 'large',
                        duration: 500,
                        animation: 'scale',
                        placement: 'left',
                        interactive: true,
                        trigger: 'click',
                        theme: 'honeybee',
                        content: document.querySelector('#tooltip_content').cloneNode(true)
                    });
      });
    });