Issue with Semantic UI causing <select> elements to not update properly

We're facing an issue with a popup that contains a form consisting of several <select> tags. Sometimes, these <select> elements appear on the page without any specified <option> tags and are filled in later on. Some already have predefined <option>s. In all scenarios, there is a possibility of adding or removing <option>s from the <select> elements. Currently, we are integrating Semantic UI to define these <select> tags as shown below:

<select id="select1" class="ui dropdown"></select>

The challenge we're encountering is that the created dropdowns (referred to as "menu") are not getting updated when changes occur in the underlying <select> element. Is there a specific function that needs to be invoked when adding or removing <option>s?

UPDATE: In an attempt to address this issue, I tested the following:

$('#select1').dropdown('refresh')

Unfortunately, the semantic UI menu did not reflect any updates.

UPDATE 2 It's worth noting that in some instances, the <option>s are toggled by changing their display property to none instead of physically removing them from the <select> tag. Conversely, in other cases, they are indeed added or removed from the list. Can Semantic UI effectively handle both of these scenarios?

Answer №1

After deciding to move forward, I took the initiative to create a proof-of-concept to explore potential solutions. Using a mutationobserver, I monitored changes in the attributes of each <select> element within the popup and synchronized the display property with the semantic ui "menu". So far, the implementation seems to be effective.

function implementSelectMutationObserver()
{
    //create observer
        var observer = new MutationObserver(function(mutations){
            //console.log(mutations)

            //get changed element in select
                var target_el = mutations[0].target

            if(!getElement(target_el.parentNode))
            {
                return false
            }


            //find the changed <select>
                var sel = target_el.parentNode

            //make sure the parent is a <select>
                if(sel.nodeName != 'SELECT')
                {
                    return false
                }

            //get <select> wrapper created by semantic ui
                var wrapper = sel.parentNode

            //find the associated semantic menu
                var cur_menu = $(wrapper).children('.menu')

            if(getElement(cur_menu))
            {

                //get corresponding element to target element in semantic menu
                    var menu_el = $(cur_menu).children('div[data-value=' + target_el.value + ']')[0]

                //change the menu element to match the style of the <select> element
                    if(menu_el)
                    {
                        menu_el.style['display'] = target_el.style['display']
                    }

            }
        })

    //initialize config to look for changes to attributes
        var observer_config = {
            attributes: true,
        }

    //set observer on each <option>
        var target_nodes = getElement('my_popup').querySelectorAll('option') //document.body
        for(var x=0;x<target_nodes.length;x++)
        {
            observer.observe(target_nodes[x], observer_config)
        }
}

function getElement(el)
{
    if(document.getElementById(el))
    {
        return document.getElementById(el)
    }
    else if($(el).get(0))
    {
        return $(el).get(0)
    }
    else if((typeof el == 'object') && (Object.keys(el).length > 0))
    {
        return el
    }
    else
    {
        //console.log(el + ' not found')
        return false
    }
}

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

Encasing event handler callback within Observable pattern

I am currently exploring how to wrap an event callback from a library to an RxJS Observable in a unique way. This library I am working with sets up handlers for events like so: const someHandler = (args) => {}; const resultCallback = (result) => {} ...

How to Achieve a Fixed Bottom Footer in Ember Framework

Hello, I am working on an Ember application and trying to implement a Sticky Footer feature. I followed a tutorial on Sticky Footer On CSS-Tricks which worked for me previously, but it seems to not work with Ember. Here is my CSS code: .mainFooter { ...

Is it true that when you return from a callback in JavaScript, it gives back

Here's the code I'm working with: function retrieveSocialCount(type, filePath, executeCallback){ var request = new XMLHttpRequest(); request.onload = function(){ if(request.status === 200 && request.readyState === 4){ ...

Struggling to make align-content function properly in a CSS flexbox layout grid

Utilizing CSS flexbox for designing a grid of items that wrap on resizing the page. Wanting to achieve a horizontally centered grid, successfully done with justify-content: center;. However, facing an issue where the last box in some cases is centered inst ...

Tips for creating a multi-tenant application using Node.js (express.js)

I need help finding information on developing a multi-tenant application using Node.js. Can anyone offer some guidance? Thank you. My technology stack includes: Node.js Express.js Mocha.js Postgres SQL JavaScript HTML5 ...

Tips for resolving issues with dynamically displaying state information based on a selected country

I'm currently working on a project that requires me to dynamically fetch the states of a specific country when a user selects the country of birth for their family members. To do this, I am utilizing AJAX. Due to limitations, I can only include detai ...

Syntax in Next.js for shallow routing

When working with next js, I want to update the route without refreshing the page. Currently, I am using the following syntax which I find cumbersome: Router.push( `/statistics?entityId=${id}&type=${entityType}&provider=${serviceProvider}`, ...

Capture video frames from a webcam using HTML and implement them in OPENCV with Python

While many may find it simple, I am facing a challenge in extracting video frames from HTML or JavaScript and utilizing them in my Python OPENCV project. I have a strong background in Python OPENCV and Deeplearning, but lack knowledge in HTML and JavaScr ...

Utilizing Gigya API integration with node.js

Looking to incorporate Gigya as my social network connection provider for a project and developing the app with Node.js. Wondering if anyone has experience with this combination? Gigya offers a JavaScript API specifically designed for client-side use. Th ...

Organized Styled-components

Considering implementing styled-components in my application and questioning the best organizational approach. Usually, styled-components are associated with a particular component for increased reusability. However, what is the recommended method for us ...

Using jQuery to apply a CSS class to a chosen radio button

How can I dynamically add a CSS class to a radio button based on its selection? $("input[type=radio][name=align]").on('change', function() { var radVal = $("input[type=radio][name=align]").val(); if (radVal == 'center') { $(" ...

Is there a way to prevent Chrome from highlighting text that matches the content of a `position: fixed` element?

I currently have a Toc element in my HTML with the property position: fixed. (This means it remains in place as you scroll) My goal is to prevent Chrome's Find Text feature from recognizing text within that Toc element. (Ideally using JavaScript) ...

What methods can be used to prevent divs from overlapping when the window size is reduced?

Creating a login page with two main content divs in a section layout. The first div contains the logo, input prompts, and login/password reset buttons. The second div serves as a footer with social media links. On a full-size window, the logo is positioned ...

Create an unordered Vue component object

In the data retrieved from the backend, there is an object containing various properties, one of which is the 'average' value. This object is ordered based on that value and when accessed through Postman, it appears as follows: ranking: ...

What could be causing the if/else block in my Redux Toolkit reducer to malfunction?

I'm currently working on a shopping cart feature where I need to increase the quantity of an item if it's already in the cart. If the same item with different sizes is added, they should be displayed separately. I've successfully implemented ...

What is the process for utilizing "yarn create <pkg-name>" command?

While browsing the Yarn blog, I came across a feature that caught my attention - yarn create. This function is similar to create-react-app. https://yarnpkg.com/blog/2017/05/12/introducing-yarn/ I decided to give it a try on my local machine by creating a ...

Tips on skipping the need to repeatedly use `@ApiProperty()` for every dto in NestJs-swagger

I'm currently exploring ways to streamline the process of specifying @ApiProperty() for each DTO. I've heard about a method involving the creation of a nest-cli.json file, where if you define Promise<DTO> in your controller within nest-swa ...

PHP scripts for performing multiplication and subtraction operations

My goal is to create a PHP script that can determine the total miles driven by a car rental customer (at a rate of 0.12 cents per mile), calculate the cost based on the number of days they rented the car ($15 per day), and display the grand total in a text ...

JavaScript - Modify input character prior to appending it to the text area

I am working on creating a virtual keyboard using jQuery. Whenever I press 'a' in the textarea, I want it to display 'z' instead. In my investigation of typing a letter in the textarea, I discovered the following sequence: keyDown ev ...

Inject CSS values dynamically

I am currently working on dynamically setting the color of a div. To provide some context, let's examine the following: <!doctype html> <head> <link rel="stylesheet" href="style.css"> </head> <body> <div clas ...