Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple tippy.js tooltips with html content

I am trying to get multiple tooltips on the same page with different HTML content using tippy.js. This content varies - it might me just image or text formatted with HTML tags or text + image(s). How can I make this work?

I tried to run this code but didn't had much success

 <a class="btn" href="#">Text</a>
<div class="myTemplate">
  <b>Text</b> <img src="https://i.imgur.com/dLcYjue.png">
</div>
 <a class="btn" href="#">Text2</a>
    <div class="myTemplate">
      <b>Text2</b>
    </div>


<script type="text/javascript">
    tippy('.btn', {
  content: document.querySelector('.myTemplate')
})
    const clone = document.querySelector('.myTemplate').cloneNode(true)
</script>
like image 493
n000000b Avatar asked Jun 28 '26 04:06

n000000b


1 Answers

maybe this helps someone getting here again:

also check the documentation: https://atomiks.github.io/tippyjs/v6/html-content/

document.querySelectorAll('button[data-template]').forEach(btn => {
    tippy(btn, {
        content(reference) {
            const id = reference.getAttribute('data-template');
            const template = document.getElementById(id);
            return template.innerHTML;
        },
        allowHTML: true
    })
})
<script src="https://unpkg.com/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/tippy-bundle.umd.min.js"></script>

<button data-template="one">One</button>
<button data-template="two">Two</button>
<button data-template="three">Three</button>

<div style="display: none;">
  <div id="one">
    <strong>Content for `one`</strong>
  </div>
  <div id="two">
    <strong>Content for `two`</strong>
  </div>
  <div id="three">
    <strong>Content for `three`</strong>
  </div>
</div>
like image 75
user1895720 Avatar answered Jun 30 '26 18:06

user1895720



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!