5

I tried to change the tooltip placement dynamically but it does not work.

<input type="text" id="sample" title="Tip">
<button name="change me" id="changeBtn">Change Tool Tip!</button>

And for js:

//Initiall tooltip for all elements
$("[title!='']").tooltip();

$("#changeBtn").click(function () {
    //Change tooltip placment
    $("#sample").tooltip({placement : 'left'}).tooltip('show');

})

http://jsfiddle.net/znvv9ar5/

I found a good answer at Change Twitter Bootstrap Tooltip content on click which shows how to change tooltip text dynamically by using tooltip('fixTitle') method. But could not find something for placement.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173

3 Answers3

8

In Bootstrap 2.x, the answer is:

$('#sample').data('tooltip').options.placement = 'right';

However, from Bootstrap 3, all Bootstrap data is namespaced with bs:

$('#sample').data('bs.tooltip').options.placement = 'right';

Code:

$("#changeBtn").click(function () {

    $('#sample').data('tooltip').options.placement = 'left';
    $("#sample").tooltip('show');

});

Play it here

void
  • 36,090
  • 8
  • 62
  • 107
2

Try using "destroy" on the tooltip and bind it again

$("#sample").tooltip("destroy").tooltip({placement : 'left'}).tooltip('show');
Arg0n
  • 8,283
  • 2
  • 21
  • 38
0

Although I do not like using !important it does help to cheat a little in this case.

.tooltip {
    left: 12px!important;
}

http://jsfiddle.net/znvv9ar5/1/ as a result.

You can apply this CSS with the on click.

Nealime
  • 169
  • 6