Bootstrap's Tooltip in general works via the title
and data-title
attributes of the element. I can specify HTML code via the data-html="true"
attribute, so for example
<a href="..." data-toggle="tooltip" data-html="true" title="<b>First Line:</b><br>Second Line.">...</a>
Which renders fine via the Tooltip plugin:
But the problem arises when JavaScript is disabled; in this case the browser shows the raw value of the title
attribute and in essence the raw HTML, which to the end user looks quite bad:
Is there a way to implement graceful degradation for tooltips containing HTML? Such that an HTML-containing value will be used by the plugin, but simple, clear-text value used by default by the browser (when Bootstrap tooltips are not enabled or when JS is not enabled).
I ended up implementing a custom transformation before enabling the Bootstrap's tooltips. The transformation removes the title
attribute from the target element and leaves only the "rich" data-title
attribute. When JS is disabled, this code will have no effect, the data-title
attribute will have no effect, and the browser will pick the plain-text title
attribute up.
$('[data-toggle="tooltip"][title][data-title]').each(function() {
this.setAttribute('data-simple-title', this.getAttribute('title'));
this.removeAttribute('title');
});
$(document).tooltip({ selector: '[data-toggle="tooltip"]' });