the functionality of the multiple dropdown in JavaScript only functions once

Hi everyone, I am new to JavaScript and I am seeking assistance with a dropdown issue I am facing. Currently, I am using a JavaScript code for multi-level dropdowns. However, when I use it for multiple dropdowns, the second dropdown follows the sequence of the first dropdown's selection.

The challenge is that each multi-level dropdown should be unique. For example:

Dropdown A: 1st level: first option 2nd level: first option content dropdown

Dropdown B: 1st level: second option 2nd level: first option content dropdown

It should ideally look like this:

Dropdown A: 1st level: first option 2nd level: first option content dropdown

Dropdown B: 1st level: second option 2nd level: second option content dropdown

Below is the JavaScript code I am currently using:

$(document).ready(function() {
  $('#multi-dropdown').bind('change', function() {
    var elements = $('div.container').children().hide(); // hide all the elements
    var value = $(this).val();

    if (value.length) { // if something is selected
      elements.filter('.' + value).show(); // show the ones we want
    }
  }).trigger('change');

  $('.second-level-select').bind('change', function() {
    var elements = $('div.second-level-container').children().hide(); // hide all the elements
    var value = $(this).val();

    if (value.length) { // if something is selected
      elements.filter('.' + value).show(); // show the ones we want
    }
  }).trigger('change');
});

You can access the code snippet here: https://jsfiddle.net/aew2960d/

Answer №1

It is important to avoid having duplicate IDs for multiple elements. Specifically, in this scenario, both dropdowns are given the same ID of multi-dropdown (as referenced in the JSFiddle link). To resolve this issue, simply modify the ID of the second dropdown and assign the corresponding event to it.

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

Reinitializing a form with jQuery validation inside a modal box

While utilizing jQuery Validate in a form within a jQuery dialog, I am encountering an issue. Upon closing the dialog, I aim to clear all form fields and reset any fields with error feedback displayed. Although the fields are being reset to blank as expec ...

Tips for retaining previous data while loading in React hooks

Just started using react hooks and built my initial component. Everything is functioning properly except for a flickering issue while the data is loading. Here is my component (a basic list of articles with previous/next pagination): function InfoFeed ({ l ...

how can a select dropdown be dynamically displayed based on the previous selection?

If the first dropdown is set to "Professor" I want to display a second dropdown, but if it is set to "Student" then I do not want to display the second dropdown. function checkPrivilege() { var privilege = document.getElementById("permisija5").value; ...

The 1and1 Server is experiencing a 500 internal error when using Ajax and jQuery .Load() functions, but accessing the URL directly seems to

Currently, I am utilizing a jQuery .load() function to enable infinite scrolling in a CakePHP web application. Below is the snippet of Javascript code: $("#post_container").append('<div class="batch row" style="display:none;"></div>&apos ...

Leverage jquery to adjust the height or width of an element depending on which dimension is larger

I'm a beginner in jquery and html and am currently developing a gallery webpage. The functionality involves setting the selected image as the page background and then scrolling vertically based on the mouse pointer's position. This works well for ...

Tips on utilizing CSS modules in React without changing class names

After starting to use css modules in my react project, I quickly realized the struggle of changing classnames to fit the requirements of css modules. For example, if we have a component using regular css: import React from 'react' import ". ...

React app experiencing issues with onClick button methods not functioning as expected

Here is the React code for a sample homepage. My intention was to have the function run and update the page when the buttons are clicked. However, instead of updating the page, it keeps showing alerts. I have confirmed that the fetch function is pulling da ...

The Excel document became corrupted after downloading through a ReactJs jQuery ajax post request

Attempting to download a file using jQuery AJAX post request from ReactJs, with an express router middleware layer utilizing the Request API to send requests to a Spring Boot REST API. The flow goes like this: AJAX => Express POST route => Spring Boo ...

What is the best way to align content in the left center of a Paper component and ensure it stays that way on smaller devices?

Recently, I've been developing a component for my Goal Sharing social media platform. Here's what I have accomplished so far: https://i.stack.imgur.com/UDRim.png In an attempt to position the Avatar component along with two typography component ...

Managing browser cache while developing locally

During development testing, how can you selectively disable caching for local scripts and styles while keeping cache enabled for external ones? Ideally in Firefox. When editing css, js, or sprite files on my developmental site, I find myself needing to fr ...

Dynamic positioning of livechatinc in real-time

Our website is currently using LiveChat from www.livechatinc.com. They insert the livechat code onto our page after it has been loaded. I am looking to dynamically position the livechat div on our page once the page is fully loaded. Attempting to achieve ...

Incorporating the outcome of an asynchronous function into my 'return' statement while iterating through an array

Issue I am Facing I encountered a problem while trying to execute a database function while simultaneously mapping an array. To illustrate this problem in a more concise manner, I have developed a sample code snippet. In my implementation, I am utilizing ...

The react component is not defined within the <Popup></Popup> tag

SOLVED: Big thanks to everyone who offered their assistance. Upon further investigation, I discovered that in the library I was using, the trigger={} functionality is specifically designed to work only with a button element. To address this issue, I took ...

How to send arrays through JSON?

I have some PHP code: $ids = array(1,2,3); $names = array("cat","elephant","cow"); $originalSettings = array ('ids'=>$ids,'names'=>$names); $jsonSettings = json_encode($originalSettings); echo $jsonSettings; Here ...

Learning how to implement the selection tag to upload data to a SQL database using PHP

As a coding beginner, I've been tackling a project involving data insertion into an SQL server with PHP. My current struggle lies in the process of inserting data and capturing select tag objects as variables. I keep encountering Undefined Index error ...

Ensure that at least one checkbox is selected by using custom validation in jQuery

My form has checkboxes for gender (male and female) and a set of checkboxes for days of the week (Monday to Sunday). I need to set validation rules so that at least one gender and one day is selected. Below is my jQuery code: $(document).ready(function( ...

Using an Angular dynamic ng-model name within an accordion

Looking for a way to make the ng-model name dynamic in this specific situation. Whenever a department is selected, the form needs to be updated accordingly. Currently, it works with ng-model="department.name", but this is causing some unexpected issues du ...

Using numerous modal windows within the .map function

I am working with a .map function and I want to create a modal in each div generated by the .map. return ( {this.state.DataBook.map(function (item, i) { return ( <div> <Button color="danger" onClick={this.toggleModal}>test Mo ...

Bootstrap 4 content block alignment explained

Below is the code snippet I am currently working with: .banner { background: #f9f9f9; width: 300px; height: 60px; } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" hre ...

Is JavaScript's setTimeout 0 feature delaying the rendering of the page?

Based on information from this StackOverflow post The process of changing the DOM occurs synchronously, while rendering the DOM actually takes place after the JavaScript stack has cleared. Furthermore, according to this document from Google, a screen r ...