Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap popover is not working

The bootstrap popover is not showing up my page

Here is my HTML:

<button type="button" class="btn btn-lg btn-danger"          data-toggle="popover" title="Popover title"         data-content="And here's some amazing content. It's very engaging. Right?">      Click to toggle popover </button> 

Here are all the js and css files I've added:

@Styles.Render("~/Content/css") @Styles.Render("~/Content/bootstrap.min.css") @Styles.Render("~/Content/bootstrap.css") @Styles.Render("~/Content/bootstrap-theme.css") @Styles.Render("~/Content/css/default.css")  @Scripts.Render("~/Scripts/jquery-2.1.1.js") @Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.js") @Scripts.Render("~/bundles/modernizr") @Scripts.Render("~/Scripts/bootstrap.js") 

Can someone tell me where is the problem?

PS: Is there a way to get the popover to work without having to write any script code?

like image 468
Rodrigo de Farias Avatar asked Oct 25 '14 12:10

Rodrigo de Farias


People also ask

How do I use bootstrap popover?

How To Create a Popover. To create a popover, add the data-toggle="popover" attribute to an element. Note: Popovers must be initialized with jQuery: select the specified element and call the popover() method.

How do I show popover in bootstrap 5?

To create a popover, add the data-bs-toggle="popover" attribute to an element. Note: Popovers must be initialized with JavaScript to work.

How do I customize bootstrap popover?

To create a popover, you need to add the data-bs-toggle="popover" attribute to an element. Whereas, popover's title and its content that would display upon trigger or activation can be specified using the title and data-bs-content attribute respectively. Here is the standard markup for adding a popover to a button.

How do I show popover on hover?

Set the trigger option of the popover to hover instead of click, which is the default one. Or with an initialization option: $("#popover"). popover({ trigger: "hover" });


2 Answers

From the Docs on Popovers:

Opt-in functionality:
For performance reasons, the Tooltip and Popover data-apis are opt-in, meaning you must initialize them yourself.

So you must call .popover() manually in JavaScript like this:

$("[data-toggle=popover]").popover(); 

Or you can use whatever selector you want

Here's an example using StackSnippets.

$("[data-toggle=popover]").popover();
body {    padding: 50px;  }
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet"/>    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>  <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>      <button type="button" class="btn btn-lg btn-danger"           data-toggle="popover" title="Popover title"           data-content="And here's some amazing content. It's very engaging. Right?">    Click to toggle popover  </button>

Note: This is similar to the answer to Bootstrap Tooltip Not Showing Up

like image 119
KyleMit Avatar answered Sep 19 '22 21:09

KyleMit


Although the accepted answer is fine, when dealing with popovers, please be careful of situations with double initialization as well (Fiddle example). The below JavaScript will fail.

<br/> <br/> <a href="#" id="firstButton" class="btn btn-primary" rel="popover" data-message="Message">Click Me (Working)</a> <br/> <br/> <a href="#" id="secondButton" class="btn btn-primary" rel="popover" data-message="Message">Click Me (Failing)</a> 

If you double-initialize and your popover uses values that may change or custom content, etc., you will be in a world of hurt:

$(function () {     $('#firstButton').popover({       container: "body",       html: true,       content: function () {         return '<div class="popover-message">' + $(this).data("message") + '</div>';       }     });     $('#secondButton').popover(); // <-- The first initializer does this, which causes the next one to fail on the next line.     $('#secondButton').popover({       container: "body",       html: true,       content: function () {         return '<div class="popover-message">' + $(this).data("message") + '</div>';       }     }); }); 
like image 35
Alexandru Avatar answered Sep 23 '22 21:09

Alexandru