Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container my-4">
  <h1 class="h3">Bootstrap tooltips &amp; popovers, and the <code>selector</code> option</h1>

  <p>This CodePen demonstrates the use of the <code>selector</code> option passed to the Bootstrap tooltip and popover JavaScript plugins.</p>

  <p>The <code>selector</code> option essentially allows you to delegate tooltips and popovers to another element, which means that you can allow dynamically added HTML with the correct selectors to trigger popups as if they were present in the originally loaded DOM. Without the selector option, only elements present in the initial DOM will trigger tooltips; any that are dynamically added will not.</p>

  <p>Toggle the checkbox below and click the "Add new popover" button to observe the behavioral differences between using the selector option, and not using it.</p>

  <hr>

  <div class="form-check">
    <label id="use-selector-label" class="form-check-label">
      <input id="use-selector" class="form-check-input" type="checkbox">
      <span data-title="You must re-run this CodePen to change the selector option once you've started the demo">Use selector option</span>
    </label>
  </div>

  <pre id="with-selector-code" class="d-none">
<code class="lang-js">new bootstrap.Popover(document.body, {
  selector: '.has-popover'
});</code>
</pre>

  <pre id="without-selector-code" class="d-none">
<code class="lang-js">document.querySelectorAll('.has-popover').forEach(function (element) {
  new bootstrap.Popover(element);
});</code>
</pre>

  <button id="add-button" class="btn btn-primary mt-2">Add new popover</button>

  <div class="buttons d-none">
    <hr>

    <button class="w-100 btn btn-primary mb-2 has-popover" data-title="Static" data-content="This button was specified in the initial HTML document" data-placement="top">Working popover</button>
  </div>
</div>

              
            
!

CSS

              
                
              
            
!

JS

              
                // Helper function
function $(selector) {
  return document.querySelector(selector);
}

// Demo-related
function usingSelectorOption() {
  return $('#use-selector').checked;
}

// Demo-related
function updateCodeView() {
  // $('#with-selector-code').classList.toggle('d-none', !usingSelectorOption());
  // $('#without-selector-code').classList.toggle('d-none', usingSelectorOption());
  if (usingSelectorOption()) {
    $('#with-selector-code').classList.remove('d-none');
    $('#without-selector-code').classList.add('d-none');
  } else {
    $('#with-selector-code').classList.add('d-none');
    $('#without-selector-code').classList.remove('d-none');
  }
}

document.addEventListener('DOMContentLoaded', function () {
  // Update code view when checkbox is toggled
  updateCodeView();

  $('#use-selector').addEventListener('click', function () {
    updateCodeView();
  });

  var demoStarted = false;

  $('#add-button').addEventListener('click', function () {
    // One-time initialization
    if (!demoStarted) {
      if (usingSelectorOption()) {
        new bootstrap.Popover(document.body, {
          selector: '.has-popover'
        });
      } else {
        document.querySelectorAll('.has-popover').forEach(function (element) {
          new bootstrap.Popover(element);
        });
      }

      demoStarted = true;
    }

    // Disable selector checkbox, put a tooltip on it, and show the buttons panel
    $('#use-selector').disabled = true;
    new bootstrap.Tooltip($('#use-selector-label span'));
    $('.buttons').classList.remove('d-none');

    // Add a new button that triggers (or doesn't) a popover, with the appropriate message
    var button;
    if (usingSelectorOption()) {
      button = '<button class="w-100 btn btn-success mb-2 has-popover" data-title="Dynamic" data-content="This button was added dynamically by JavaScript" data-placement="top">Working dynamically added button</button>';
    } else {
      button = '<button class="w-100 btn btn-secondary mb-2 has-popover" data-title="Dynamic" data-content="This button was added dynamically by JavaScript" data-placement="top">Non-working dynamically added button</button>';
    }

    $('.buttons').insertAdjacentHTML('beforeend', button);
  });
});

              
            
!
999px

Console