Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind bootstrap tooltip on dynamic elements

When using Bootstrap tooltip, we need to write something like this to have tooltip functionality:

$(".showtooltip").tooltip();

But the problem is, we need to rerun that code every time when new content is loaded via ajax. Is there anyway to run the code for once and auto apply the tooltip when new element is loaded?

Thank you.

like image 923
user1995781 Avatar asked Jul 09 '14 13:07

user1995781


People also ask

How do I display dynamic content in tooltip?

How do I display dynamic content in tooltip? The tooltip content can be changed dynamically using the AJAX request. The AJAX request should be made within the beforeRender event of the tooltip. On every success, the corresponding retrieved data will be set to the content property of the tooltip.

How do I change dynamic value tooltip?

Here's how: Drag the calculated field to the appropriate tooltip and you'll see an ATTR dimension pill with a tooltip logo in the Marks card. Insert the ATTR budget and adjusted inflated gross calculated fields into its corresponding tooltip as seen in Image 6. After that, you're dynamic tooltip should work!

Can tooltips be clickable?

As we mentioned in the preamble, tooltips are completely "mute" to any interactions as to not steal hover events from elements underneath them. Even placing interactive elements, such as links, into content of tooltip will not work.

What are the ways to enable Bootstrap tooltip plugin for the HTML elements?

To create a tooltip, add the data-toggle="tooltip" attribute to an element. Note: Tooltips must be initialized with jQuery: select the specified element and call the tooltip() method.


2 Answers

You need to use selector property. See on the documentation :

"If a selector is provided, tooltip objects will be delegated to the specified targets. In practice, this is used to enable dynamic HTML content to have tooltips added. See this and an informative example."

JS example code :

$('body').tooltip({
    selector: '.createdDiv'
});

$('#add-button').click(function() {
    $('<div class="createdDiv" data-toggle="tooltip" title="Some tooltip text!">Hover over me</div>').appendTo('#container');
});

DEMO

like image 102
Elfayer Avatar answered Oct 16 '22 07:10

Elfayer


Try this:

$('body').tooltip({
    selector: '[data-toggle="tooltip"]'
});
like image 28
Aliti Avatar answered Oct 16 '22 07:10

Aliti