This is a follow up to my previous question.
I need to place Bootstrap tooltips dynamically. To do so, I need to know the position of the element which triggers the tooltip (works fine) AND the width of the tooltip which is supposed to show up:
Edit for clarification: My tooltips are generated dynamically and then inserted into the content. If they are, for example, too close to the left edge of the screen I need to place them 'right' instead of 'top'. Now I want to get the width of the tooltip to only place it 'right' when it would actually go out of the screen.
$('body').tooltip({
delay: { show: 300, hide: 0 },
selector: '[rel=tooltip]:not([disabled])',
placement: function(tip, element) {
var offsetLeft = $(element).offset().left;
var offsetRight = ( $(window).width() - ( offsetLeft + $(element).outerWidth() ) );
// need help here: how to get the width of the current tooltip?
// currently returns "0" for all elements
var tooltipWidth = $(tip).outerWidth();
console.log(tooltipWidth);
if (offsetLeft < 220) {
return 'right';
}
if (offsetRight < 220) {
return 'left';
}
else {
return 'top';
}
}
});
Thanks in advance.