Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting an element with jQuery in a Twitter Bootstrap Popover

I'm placing the following content in a Twitter Bootstrap popover, which contains a link for which I want to listen for clicks:

<div id="popover-content">
  <a id="link" href="#">click</a>
</div>

I'm using a button to reveal the popover which contains the above content:

<button id="trigger" data-placement="bottom" title="title">Reveal popover</button>

I then associate the button with the popover and use jQuery's click() function in attempt to listen for clicks on the link contained in the popover:

$(function(){
  $('#trigger').popover({ 
    html: true,
    content: function() {
      return $('#popover-content').html();
    }
  }); 

  $('#link').click(function() {
    alert('beep');
  });
});

However, upon clicking the button to reveal the popover and then clicking the link, the click seems to not be detected as intended above. My understanding of the DOM and javascript and jQuery is fairly limited, so I'm not sure what's going on here. How can you select/listen for actions on elements contained in a popover?

Reference: Popovers in Bootstrap

like image 591
TPoy Avatar asked May 11 '13 06:05

TPoy


People also ask

How do I show Popovers in jQuery?

To show or hide the Popover programmatically, call the show() or hide() method. The same thing can be done using the toggle(showing) method. Pass true or false to this method to show or hide the Popover, respectively. This approach is more typical of jQuery Controls.

How do I use popover in bootstrap?

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 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.

What is the difference between tooltip and popover?

Tooltip: use tooltips to show a short text to respondents when they hover over a word or icon. Popover: use popovers to show a longer text, or when you want to have a link to an external web page. It is shown when the respondent clicks on a word or icon.


2 Answers

You do not need to perform Event delegation here.Instead use $('#popover-content') instead of $('#popover-content').html() while setting the content. This will have the events registered attached by default without requiring any delegation.

Demo

$(function(){
  $('#trigger').popover({ 
    html: true,
    content: function() {
      return $('#popover-content'); //<-- Remove .html()
    }
  }); 

  $('#link').click(function() {
    alert('beep');
  });
});
like image 71
PSL Avatar answered Oct 18 '22 03:10

PSL


You can try this:

$(document).on('click', '#link', function(){
    alert('beep');
});
like image 28
vher2 Avatar answered Oct 18 '22 02:10

vher2