How to insert HTML elements into a Tooltip Bootsrap?

Asked

Viewed 1,187 times

2

I have a code that generates a tooltip with a small message and makes disappear after 2.5s.

var msg ="Oops! Essa é uma mensagem pequena";
$('#addItInfo').attr('data-original-title',msg);
$('#addItInfo').tooltip('show');
setTimeout( function(){ 
   $('#addItInfo').tooltip('hide');
   $('#addItInfo').removeAttr('title');
   $('#addItInfo').removeAttr('data-original-title'); 
}  , 2500 ); //Wait 2,5 seconds  

The code works well, however I would like to dynamically insert some HTML elements inside this tooltip, so I can bring some database information when the user hovers the mouse on the element that contains the tooltip. I took a test with a div and so I created this message just for test:

var msg ="Essa é uma mensagem teste <div>elemento div interno</div>";

Unfortunately the tooltip element does not display as expected.

Here is a small example of how I would like it. In this example I inserted a table inside the tooltip that is not bootstrap (only to test if it is possible) and that it is not well formatted (has other problems).

So my question is: how to insert html elements inside a bootstrap tootip? Any ideas?

  • popover would be more appropriate for what you want. Just activate it at Hover. trigger: 'hover'

  • Yes Dap.Tci, I was really looking for another way to present this data and Popover seems to be what I was looking for. I will do my tests with the tooltip, but I will be really [and with the Popover. Thanks!

1 answer

2


Answer to your question (JSFIDDLE):

$('body').tooltip({
    selector: '[data-toggle="tooltip"]',
    html: true,
    title: $('#tooltip-table').html(),
    placement: 'right'
});
.tooltip, .tooltip-inner {
    min-width: 320px !important;
    width: 320px !important;
}
<div class="container">
    <a data-toggle="tooltip" href="javascript:;">Tooltip</a>
    
    <div id="tooltip-table" class="hide">
        <table class="table table-bordered" style="width: 300px;">
            <caption>Monthly savings</caption>
            <tr>
                <th>Month</th>
                <th>Savings</th>
            </tr>
            <tr>
                <td>January</td>
                <td>$100</td>
            </tr>
            <tr>
                 <td>February</td>
                 <td>$50</td>
             </tr>
        </table>
    </div>
</div>

Tip: I think I’d better use the popover(JSFIDDLE):

$('[data-toggle=popover]').popover({
   content: $('#popover-table').html(),
   html: true,
   trigger: 'hover'
});
<div class="container">
    <h3>Exemplo de <i>popover</i> com elementos HTML</h3>
    <a href="javascript:;" data-toggle="popover">
        <span>Popover</span>
    </a>

    <div id="popover-table" class="hide">
        <table class="table table-bordered">
            <caption>Monthly savings</caption>
            <tr>
                <th>Month</th>
                <th>Savings</th>
            </tr>
            <tr>
                <td>January</td>
                <td>$100</td>
            </tr>
            <tr>
                 <td>February</td>
                 <td>$50</td>
             </tr>
        </table>
    </div>
    
</div>

  • 1

    Thanks Eduardo, you even improved the second part of the code. Really Popover is more appropriate.

  • 1

    Glad I could help! Good luck!!!

  • But just to leave it documented for those who need it later I found another way to activate the html display in the tooltip. Just insert data-html="true" in the element that will show the tooltip. It was in the official documentation and I didn’t see it. http://getbootstrap.com/javascript/#tooltips

  • 1

    Ah, yes!!! Is that method using the attributes data- It’s better when you use pure HTML. To "centralize" the programming that uses JS, I put it all in Jquery. Notice that I could use everything directly inside HTML, but you would have a horrible, hard-to-read code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.