The Select2 dropdown does not function correctly within the Bootstrap popover

My concern is about integrating a select2 dropdown into my popover. The issue I am facing is that when I open the popover for the first time, the select2 dropdown renders correctly. However, upon closing and reopening the popover, it displays two select2 dropdowns, with one being unclickable:

https://i.sstatic.net/Xok8I.png

I have attempted the z-index solution suggested in this Stack Overflow thread as well as the solution mentioned in this other thread, but unfortunately, neither of them resolved the issue. I also explored the possibility of using the dropdownParent property to render it properly within Bootstrap modals, however, that did not work either.

Snippet of My HTML Structure:

<div class="actions-row mb-5">
   <button type="button" class="btn popover-toggle add-to-program-popover" data-placement="bottom">
   Add to program
   </button>
   <div class="program-container" style="display: none">
      <select name="movement-program-select" class="movement-program-select">
         <option value=""></option>
         <option value="Program 1">Program 1</option>
         <option value="Program 2">Program 2</option>
         <option value="Program 3">Program 3</option>
      </select>
      <i class="fa fa-floppy-o add-to-program" aria-hidden="true"></i>
      <hr>
      <a class="create-new-program">
      <i class="fa fa-plus-square-o add-item"></i>
      Create new program</a>
   </div>
</div>

This is the JS Code I'm Using:

 $('button.add-movement-to-program').on('click', function(e) {
   $('.add-to-program-table-container').show();
 });

 $('button.add-to-program-popover').popover({
   container: 'main',
   html: true,
   content: function() {
     return $('.program-container').html();
   }
 });

 $('button.add-to-program-popover').click(function() {
  // console.log(1);
  // if (!$('.movement-program-select').hasClass("select2-hidden-accessible")) {
  // console.log(2);
  $('.movement-program-select').select2({
    container: 'body',
    width: "100%",
    dropdownParent: $('.popover-content')
  });
  // }
});

Can anyone suggest alternative solutions to fix this issue?

Here is the link to my JSFiddle for reference: http://jsfiddle.net/fpk047rh/1/

Answer №1

If you're looking to utilize the show event for specifically targeting the select element within your popover, here's a helpful example.

Take a look at this sample setup:

HTML:

<div id="wrapper">
  <button type="button" id="myButton" class="btn btn-lg">Click to toggle popover</button>
  <div id="form" style="display:none;">
    <select class="select-program" name="program">
      <option value=""></option>
      <option value="Program 1">Program 1</option>
      <option value="Program 2">Program 2</option>
      <option value="Program 3">Program 3</option>
    </select>
    <div class="add-program"></div>
  </div>
</div>

JavaScript:

$(function () {
  $("#myButton").popover({
    html: true,
    content: $("#form").html(),
    placement: "bottom"
  });

  $("#myButton").on("show.bs.popover", function () {
    setTimeout(() => {
      $(".popover-content .select-program")
        .select2({
          dropdownParent: $(".popover-content")
        })
        .on("select2:select", function (e) {
          var data = e.params.data;
          $(".popover-content .add-program").html("Add " + data.text);
        });
    }, 0);
  });
});

For further details and guidance, check out more information here.

Explore the code in action on CodePen

Answer №2

I was able to resolve the issue using the inserted.bs.popover event:

$('button.add-to-program-popover').popover({
  container: 'body',
  html: true,
  content: function() {
    return $('.program-container').html();
  }
});

$('button.add-to-program-popover').on('inserted.bs.popover', function () {
  $('div.popover-content select').select2({
    dropdownParent: $('.popover-content')
  });
});

Additionally, the select box that I am converting into a select2 dropdown is located within the popup container ($('div.popover-content select'))

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The collapse menu toggle feature seems to be malfunctioning, as it is not functioning properly

I've been working on a website at , where the toggle menu is causing the content to hide when the menu is expanded. <aside id="layout-menu" class="layout-menu menu-vertical menu bg-menu-theme active"> <div class= ...

The URL remains unchanged even after clicking the search button using the post method

Whenever I visit a page with URL parameters, such as , and perform a search using the search button with form method = post, the URL maintains the previous parameter values even after displaying the search results. Even though it shows the search result, ...

How do I initiate PUT and DELETE requests from my HTML code?

I am currently in the process of developing a web application that will manage items within a list. Previously, I used buttons with event listeners and JavaScript functions to handle editing and deleting items. However, I am now transitioning towards build ...

Custom directives are unable to interact with inbuilt directives

var app = angular.module("myDiscuss", []); app.controller("TabController", function() { this.tab = 0; this.subTab = 0; this.like = 0; this.selectLike = function(setTab) { this.like= setTab; }; this.selectTab = function(setTab) { this. ...

Leveraging Angular 4 component as a custom widget within a static website

Is it possible to integrate my Angular 4 component (a mail window widget application built with webpack) into a static website, ideally utilizing the <script> tag? I came across some blog articles, but they all mention using SystemJS while my app is ...

Managed the double-click event to select Snap.svg text

I am currently utilizing the snapsvg library for a project where I am implementing the dblclick event to trigger a browser window alert. However, when clicking on the svg canvas, not only does the alert pop up but some text on the canvas also gets selected ...

What is the reason for typescript's lack of a "function" type?

As a newcomer to TypeScript, I'm puzzled as to why I am unable to define an object like this: const obj: { property1: string property2: boolean property3: function } It seems that the only workaround is to use: const obj: { property1: strin ...

placing circular button beside pick component

I have created a code that retrieves json from a backend api and fills a select component in vue. I'm looking to include a reload button beside the select component, <!DOCTYPE html> <html lang="en"> <title>Test page app</title ...

What is the process for converting and transferring the date in Google Apps Script to generate a new event in Google Calendar?

To create an event in GAS, follow this link: https://developers.google.com/apps-script/reference/calendar/calendar#createEvent(String,Date,Date,Object) var event = CalendarApp.getDefaultCalendar().createEvent('Apollo 11 Landing', new Date(& ...

What could be causing my ajax post function to malfunction when triggered by a button click event?

My attempts to send variables to a PHP file via AJAX when a button is clicked have been unsuccessful. Upon checking my PHP page, I noticed that the variables were not being received. $(document).ready(function(){ $("#qryBtn").click(function(){ ...

Implement a customized toString method for components in ReactJS

Looking to modify the toString method of a class component in reactjs? Check out the code snippet below class C1 extends React.Component{ render(){ return ( <div> {C2.toString()} </div> ) } } class C2 extend ...

Dynamic change in the mutation observer is not causing the callback to trigger

I have created a nested structure with two <div> elements. I am monitoring changes in the width of the inner <div>, which is set to width:auto;. Despite adjusting the width of the parent <div>, the mutation observer callback doesn't ...

Steps to showcase specific data in the bootstrap multi-select dropdown menu sourced from the database

Utilizing CodeIgniter, I have set up multiple select dropdowns using bootstrap. The issue I am facing is with displaying the selected values in these drop-downs on the edit page. Would appreciate some guidance on how to tackle this problem. Here is a sni ...

Tips for updating multiple documents in a MongoDB database using an array of values

I have a range of Product items in my inventory and when a client places an Order, they submit an array of objects. Each object in the array includes the _id product (objectId) and the quantity they purchased. [ {producId: "dshsdsdh72382377923", quantity: ...

What is the code to automatically change the selected input radio button every 3 seconds?

I am attempting to cycle between different input radio buttons being checked every 3 seconds in my Next.js project. The switching works from case0 to case1 and case1 to case2, but not from case2 back to case0. My React and Next.js knowledge is at an inte ...

What is preventing me from installing the "express" package?

After successfully installing Node.js, npm, and Express on my machine, I encountered an issue when trying to install Express in my project directory. lds-MacBook-Pro:contacts ldnwty$ npm install express npm WARN package.json <a href="/cdn-cgi/l/email-p ...

Setting up popover functionality in TypeScript with Bootstrap 4

Seeking assistance with initializing popovers using TypeScript. I am attempting to initialize each element with the data-toggle="popover" attribute found on the page using querySelectorAll(). Here is an example of what I have tried so far: export class P ...

AngularJS - make it a practice to set a default parameter for every Resource

I have encountered a situation where the API I am working with requires a :groupId parameter in the URL for many operations. Is there a simple way to eliminate the need to pass $routeParams.groupId in every function call? var app = angular.module('pl ...

Dealing with the issue of receiving raw JavaScript in the .ajax error function even when receiving a 200

I am encountering an issue with a jQuery.ajax() call to a third-party product. The POST request is successful, returning a 200 OK status code, but instead of executing the success function, it redirects to the error function. The response variable displays ...

"Transferring a JavaScript variable to Twig: A step-by-step guide for this specific scenario

When it comes to loading a CSS file based on the user's selected theme, I encountered an issue while trying to implement this in my Symfony application using Twig templates. The code worked flawlessly on a simple HTML page, but transferring it to a Tw ...