How to Use Complex HTML with Twitter Bootstrap's Tooltip

Can I use complex HTML with Twitter Bootstrap's Tooltip?

This parameter is just about whether you are going to use complex html into the tooltip. Set it to true and then hit the html into the title attribute of the tag.

See this fiddle here - I've set the html attribute to true through the data-html="true" in the <a> tag and then just added in the html ad hoc as an example.

Is there any way to support HTML tags code inside bootstrap Tooltip?

For getting html formatted result on tooltip you have to use html: true when initialize the bootstrap tooltip

Example:
$('[data-toggle="tooltip"]').tooltip({ html: true });

bootstrap 4 tooltip with br doen't work properly

According to this page the behaviour is depending on an extra data attribute:
https://getbootstrap.com/docs/4.0/components/tooltips/

Add this to your anchor tag and it will work:
data-html="true"

<!DOCTYPE html><html lang="en"><head>  <title>Bootstrap Example</title>  <meta charset="utf-8">  <meta name="viewport" content="width=device-width, initial-scale=1">  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script></head><body>
<div class="container"> <h3>Tooltip Example</h3> <a href="#" data-toggle="tooltip" data-html="true" title="Hooray! <br> test">Hover over me</a></div>
<script>$(document).ready(function(){ $('[data-toggle="tooltip"]').tooltip(); });</script>
</body></html>

HTML-tags in tooltip

Add the attribute data-html="true" to your link

Change Twitter Bootstrap Tooltip content on click

Just found this today whilst reading the source code. So $.tooltip(string) calls any function within the Tooltip class. And if you look at Tooltip.fixTitle, it fetches the data-original-title attribute and replaces the title value with it.

So we simply do:

$(element).tooltip('hide')
.attr('data-original-title', newValue)
.tooltip('fixTitle')
.tooltip('show');

and sure enough, it updates the title, which is the value inside the tooltip.

A shorter way:

$(element).attr('title', 'NEW_TITLE')
.tooltip('fixTitle')
.tooltip('show');

HTML inside Twitter Bootstrap popover

You cannot use <li href="#" since it belongs to <a href="#" that's why it wasn't working, change it and it's all good.

Here is working JSFiddle which shows you how to create bootstrap popover.

Relevant parts of the code is below:

HTML:

<!-- 
Note: Popover content is read from "data-content" and "title" tags.
-->
<a tabindex="0"
class="btn btn-lg btn-primary"
role="button"
data-html="true"
data-toggle="popover"
data-trigger="focus"
title="<b>Example popover</b> - title"
data-content="<div><b>Example popover</b> - content</div>">Example popover</a>

JavaScript:

$(function(){
// Enables popover
$("[data-toggle=popover]").popover();
});

And by the way, you always need at least $("[data-toggle=popover]").popover(); to enable the popover. But in place of data-toggle="popover" you can also use id="my-popover" or class="my-popover". Just remember to enable them using e.g: $("#my-popover").popover(); in those cases.

Here is the link to the complete spec:
Bootstrap Popover

Bonus:

If for some reason you don't like or cannot read content of a popup from the data-content and title tags. You can also use e.g. hidden divs and a bit more JavaScript. Here is an example about that.

Modifying Twitter Bootstrap's Tooltip Colors Based on Position

Twitter bootstrap does not have this feature built in, but you could add your own functions to do this, like so:

$('#photo1').hover(function() {$('.tooltip').addClass('tooltipPhoto')}, function () {$('.tooltip').removeClass('tooltipPhoto')});​

and then you just have to define tooltipPhoto class in CSS to have a different background color.

EDIT:
Updated solution:

function changeTooltipColorTo(color) {
$('.tooltip-inner').css('background-color', color)
$('.tooltip.top .tooltip-arrow').css('border-top-color', color);
$('.tooltip.right .tooltip-arrow').css('border-right-color', color);
$('.tooltip.left .tooltip-arrow').css('border-left-color', color);
$('.tooltip.bottom .tooltip-arrow').css('border-bottom-color', color);
}

$(document).ready(function () {
$("[rel=tooltip]").tooltip();
$('#photo1').hover(function() {changeTooltipColorTo('#f00')});
$('#photo2').hover(function() {changeTooltipColorTo('#0f0')});
$('#photo3').hover(function() {changeTooltipColorTo('#00f')});
});


Related Topics



Leave a reply



Submit