Bootstrap - Programmatically attach and show popover to an element

iltdev picture iltdev · Feb 6, 2015 · Viewed 11.2k times · Source

I'm using Bootstrap v3 and I'm trying to show a popover programmatically, next to an element, when the page loads. There is no popover markup in the DOM. I want to create and show it in JQuery.

I've tried this but it doesn't work.

$( document ).ready(function() {
   $('.theElement').popover({
            placement:'right',
            trigger:'manual',
            html:true,
            content:'popover content'
        });
   $('.theElement').popover('show')
});

I get a "TypeError: Argument 1 of Window.getComputedStyle is not an object" error in console. I'm assuming this is caused by the above.

Answer

Deyson picture Deyson · Apr 27, 2016

Try this

$( document ).ready(function() {
   $('.theElement').attr('data-placement','right')
   //add all atributes.....

   //and finally show the popover
   $('.theElement').popover('show')
});