Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bootstrap popover preventDefault for click is not working in a Rails 3.2 app

Someone else asked this question here, but there was no answer or solution given.

These bootstrap files are listed at the top of my application.js file:

...
//= require bootstrap-tooltip
//= require bootstrap-popover
...

My bootstrap_additions.js.coffee file contains:

$("a[rel=popover]").popover()
$(".tooltip").tooltip()
$("a[rel=tooltip]").tooltip()

In a view I have:

<a href="#" class="btn" rel="popover" title="Title" data-content="Some content.">click</a>

When i enter localhost:3000/assets/application.js in the browser, the bootstrap-popover.js content is present. In addition i found the following:

jQuery(function() {
    $("a[rel=popover]").popover().on('click', preventDefault());
    $(".tooltip").tooltip();
    return $("a[rel=tooltip]").tooltip();
});

When the link is clicked the browser display is moved to the top of the page. When I scroll back down to the link, the popover is displayed. All is working except preventDefault. What am I missing?

Thanks.


UPDATE: To keep things clean in my code, i found the coffeescript version of the selected answer:

$("a[rel=popover]").popover().click (e) => e.preventDefault()
like image 296
Jay Avatar asked Nov 14 '12 05:11

Jay


People also ask

Why is my bootstrap popover not displaying the HTML source?

When I use the native Bootstrap PopOver with HTML in it's content, the frontend is not displaying that PopOver and the HTML Source is malformatted. Make sure to add the JS to the Toolset > Layouts JS/CSS section and to use jQuery instead of $. This support ticket is created 5 years, 3 months ago.

How to create a popover using jQuery?

How To Create a Popover. To create a popover, add the data-toggle="popover" attribute to an element. Use the title attribute to specify the header text of the popover, and use the data-content attribute to specify the text that should be displayed inside the popover's body: Note: Popovers must be initialized with jQuery: select ...

What is the difference between tooltips and popovers?

The Popover plugin is similar to tooltips; it is a pop-up box that appears when the user clicks on an element. The difference is that the popover can contain much more content. Click To Toggle Popover Click To Toggle Popover

How do I close the Popover when Clicking outside the element?

However, you can use the data-bs-trigger="focus" attribute which will close the popover when clicking outside the element: Tip: If you want the popover to be displayed when you move the mouse pointer over the element, use the data-bs-trigger attribute with a value of " hover ":


3 Answers

Using Twitter Bootstrap Popover

Updated to be in Coffeescript

1st approach

Instantiate

$("a[rel=popover]").popover()

Handle

$("a[rel=popover]").click (event) ->
  event.preventDefault()
  event.stopPropagation()
  $(this).popover "show"

2nd approach

Taken directly from their source code:

$("a[rel=popover]").popover().click (e) ->
  e.preventDefault()
like image 159
EasyCo Avatar answered Sep 21 '22 15:09

EasyCo


You can also chain as this :

$("a[rel=popover]")
  .on('click',function(e){
    e.preventDefault();
  })
  .popover();
like image 42
TontonBlach Avatar answered Sep 19 '22 15:09

TontonBlach


You can use a 'span' tag instead of an 'a' tag so that you don't need to preventDefault.

also prevent default should be associated with an event.

http://api.jquery.com/event.preventDefault/

like image 31
Jacob Avatar answered Sep 23 '22 15:09

Jacob