Use JavaScript and jQuery to dynamically fill 4 dropdown menus with the available choices

Currently, I am facing a challenge with 4 drop-down lists where each one should display the same 5 options excluding those already selected in the preceding drop-down list. Furthermore, these options need to be able to show up again if any of them are unselected at a later stage. Ideally, I would prefer to achieve this using CSS, JavaScript, and/or jQuery only.

Your assistance is greatly appreciated.

Answer №1

Master the art of achieving it all through JQuery.

Check out this helpful tutorial for guidance

The key is to populate all dropdown lists and then remove the selected option from the rest.

If removing the option proves challenging, you always have the option to disable it instead.

Answer №2

Give this a shot:

let dropdownIds = new Array();
let optionList = new Array();

dropdownIds.push(menuId1);
....
....
dropdownIds.push(menuId2);

optionList.push({'value':val1, 'name':choice1});
....
....
optionList.push({'value':val5, 'name':choice5});

/* Invoke this function when the first dropdown menu (menuId1) changes */
function populateDropdowns(dropdownIds, optionList)
{
    for (let k = 1; k < dropdownIds.length; k++)
    {
        let currentChoice = $("#"+dropdownIds[k-1]).val();
        $("#"+dropdownIds[k]).empty();
        for (let m = 0, len = optionList.length; m < len; m++){
            if (currentChoice != optionList[m].value)
            {
                let newOption = $('<option />');
                newOption.attr('value', optionList[m].value).text(optionList[m].name);
                $('#'+dropdownIds[k]).append(newOption);
             }
         }
    }
}

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

Unable to assign a value to a data attribute using jQuery

For some unknown reason, the code $('element').data('data_attribute_name', 'value'); is not functioning properly in this scenario: Here is the HTML structure: <ul class="main_menu__list" data-scheme="light"> ... </u ...

CSS and JavaScript Nav Menu Collapse (No Bootstrap)

I have written a navbar code using pure HTML/SASS, but I am facing a challenge in adding a collapse element to the navigation bar. Despite trying various solutions from Stack Overflow, I still haven't found one that works for me. Therefore, I am rea ...

Encountering an issue with getDerivedStateFromProps in React where an error states: Unable to retrieve property 'setState' of null

Just found out that componentWillReceiveProps is deprecated and now we should be using the getDerivedStateFromProps lifecycle method. You can find more information about it at this link. This is how I'm implementing it: class Main extends Component ...

How to use JQuery to monitor and detect alterations in an iFrame's content

Essentially, I am using a WYSIWYG Editor that replaces the textarea in my form with a contenteditable=true iframe. I am attempting to detect any changes made to the content within the iframe to implement an auto-update function that I have created. This f ...

What is the best way to include an icon in a Vue child component?

Struggling to implement the image in the template using both the img tag and as a background. Despite having the correct path to the icon, neither method seems to work. Check out the code snippet here <div> <a href="#" class="icon" > ...

Opening a browser tab discreetly and extracting valuable data from it

Greetings to the experts in Chrome Extension development, I am exploring ways to access information from a webpage without actually opening it in a separate tab. Is there a method to achieve this? Here's the scenario: While browsing Site A, I come a ...

Are you looking to incorporate responsive wells into your Bootstrap Twitter design?

Imagine you have this structure: <div class="container"> <div class="row-fluid swell"> <div class="span9"> hello world </div> </div> </div> And this custom styling (using twitter-bootstrap as a base ...

What is the operational mechanism behind drag and drop page builders?

I am currently delving into my inaugural MERN project, where the functionality requires specific components (such as a checkbox to-do list, images, text, etc...) that empower users to construct various pages, larger multi-checkbox aggregation lists, and be ...

Managing an undetermined quantity of elements from a form in PHP

Currently developing an inventory page for my job. I've crafted a page that will iterate through my database and showcase all the items stored within. include 'auth.php'; //authentication required for login change $sql="SELECT * FROM `inven ...

The issue of Bootstrap's closed.bs.alert event not triggering when a dynamically created alert is being closed

On my page, users can search for information, triggering an alert at the top to inform them that the search is in progress. Once the results are displayed, if a user clicks the dismiss button on the alert, the main page should reload. The issue I'm f ...

In Javascript, how are instance members within a constructor accessible to methods defined on constructor prototypes?

As I dive into learning about prototypes in JavaScript and the prototype chain, a particular issue has me puzzled. Consider this constructor: function Circle() { this.radius = 1; } let c1 = new Circle(); Circle.prototype.toString = function() { ...

Retrieve the content from paragraph elements excluding any text enclosed within span tags using the Document.querySelector method

Exploring the following element structure: <p> <span class="ts-aria-only"> Departure </span> RUH <span class="ts-aria-only">&nbsp;Riyadh</span> </p> In an attempt to extract the text RUH, it has been disc ...

Exploring the ins and outs of webpage loading speed

I am working on writing JavaScript code that includes a button to open a webpage of my choice. I now want to understand how to detect when the page I called is finished loading. Any suggestions or ideas on this topic? I apologize if my explanation was no ...

Fullcalendar feature that restricts users from selecting multiple days for an event

I am using the fullcalendar plugin from http://fullcalendar.io/ and I want to restrict users from creating events spanning multiple days. $('#calendar').fullCalendar({ defaultView: 'agendaWeek', lang: "fr", header: ...

Ways to animate a main element independently of its counterpart's animation

I want to create an animation for a parent element without affecting the child's animation. My HTML & CSS structure is set up as follows: .parent{ background-image: url('https://pm1.narvii.com/6195/421ddbf8c9a2fb1715ef833f869164dc1be ...

How to add a jQuery function to a Rails 3 if statement?

I followed a Rails 3 tutorial on creating a multi-step form which you can check out here. Additionally, I incorporated Stripe payment functionality in my app using another railscast found here. Within my application, the payment form is hidden using jQuer ...

Having trouble accessing Blockly methods in index.html, despite functioning properly in other .js files where the workspace is displayed

Currently, I'm in the process of developing a unique tool that allows users to create HTML pages using Blockly blocks. Despite successfully setting up a workspace with my custom block, I am encountering issues when attempting to extract code from the ...

Reloading table following completion of ajax POST call

When making a POST request from ajax to Python Flask, the following code is used: function humval(val1,val2){ var dict = {"url":$('#url'+val1).val(),"status":val2}; $.ajax({ url: '/testroutez&a ...

Ways to create a flexbox that can scroll and includes two divs that fill the entire screen

I need to create a flexbox layout with two divs that split the screen evenly. However, I want the flexbox to change direction from row to column and make each div full width and height of the screen when the viewport is smaller than 800px, creating a scrol ...

Ways to insert text inside a border

I'm struggling to figure out how to insert text within a border. Can someone guide me on how to accomplish this? Is there a way to place text in the center of a border? I have included a screenshot for reference. ...