Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is html title attribute and twitter bootstrap data-original-title mutually exclusive?

I have an HTML div element that when clicked displays a twitter bootstrap popover. Current code looks like this:

<div class="popover" title="supplementary info about html element" 
  data-original-title="popover title" data-content="popover content...">
</div>

$(document).on('ready', function() {
  App.Utils.Popover.enableAll();
});

App.Utils.Popover = {
  enableAll: function() {
    $('.popover').popover(
      {
        trigger: 'click',
        html : true,
        container: 'body',
        placement: 'right'
      }
    );
};

Problem is that twitter bootstrap takes the title attribute value and displays that as the title of the popup instead of using data-original-title. Any way to make the two work together as intended?

like image 353
keruilin Avatar asked Mar 18 '13 21:03

keruilin


People also ask

Why title attribute is used in HTML?

HTML title Attribute The title attribute specifies extra information about an element. The information is most often shown as a tooltip text when the mouse moves over the element. The title attribute can be used on any HTML element (it will validate on any HTML element.

Is title a global attribute in HTML?

The title attribute is part of the Global Attributes, and can be used on any HTML element.

How do you style a title attribute in HTML?

You can't style an actual title attribute How the text in the title attribute is displayed is defined by the browser and varies from browser to browser. It's not possible for a webpage to apply any style to the tooltip that the browser displays based on the title attribute.

How do I stop the title attribute from displaying tooltip?

There's no way to disable the default browser behaviour, which is to show the title attribute as a "tooltip" in the browser itself. You'll need to resort to some javascript, it could even be as simple as setting the title to blank on hover, and replacing it on mouse out....


2 Answers

This is because the popover javascript extends the tooltip javascript, and the tooltip javascript was (i believe) intended to replace the default tooltip that is set by the title attribute.

This code is the culprit (in bootstrap-tooltip.js, like 253ish)

, fixTitle: function () {
  var $e = this.$element
  if ($e.attr('title') || typeof($e.attr('data-original-title')) != 'string') {
    $e.attr('data-original-title', $e.attr('title') || '').attr('title', '')
  }
}

Where if there is a title attribute then the data-original-title attribute is replaced by the title attribute.

Edit Basically my answer would be there is no easy way. You would have to mod the bootstrap js a bit though I wouldn't really recommend that in this case. Maybe use an older version of the bootstrap popover that might not have that code in there?

like image 154
hajpoj Avatar answered Oct 04 '22 20:10

hajpoj


I had the same problem and wasn't able to use a different Bootstrap version, so I decided to inject my function into the popover prototype. Do not try this at home:

<script src="bower_components/bootstrap-sass/js/bootstrap-popover.js"></script>
<script type="text/javascript">
    // dirty hack
    $.fn.popover.Constructor.prototype.fixTitle = function () {};
</script>

Now you can add a title for the popover and a title for the browsers mouseover:

<i data-placement="bottom" data-trigger="click"
   bs-popover="'views/partials/notifications.html'" data-html="true" 
   data-unique="1" 
   data-original-title="This title will be used by the popover" 
   title="This title will be used by a browser for a mouseover"
/>
like image 33
Sonata Avatar answered Oct 04 '22 22:10

Sonata