Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically change bootstrap tooltip placement

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.

like image 295
Alireza Fattahi Avatar asked Nov 18 '15 13:11

Alireza Fattahi


1 Answers

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

like image 150
void Avatar answered Nov 05 '22 09:11

void