Generate different choices in a DropDownList depending on the selection made in another DropDownList using JavaScript

I am currently working on implementing a search panel that will populate items based on the minimum and maximum price criteria. When looking at the front view of my page, it appears as follows:

For instance, if I select a minimum price value of 100,000, then the maximum price should display as >=150,000 (showing only 150,000 & 200,000).

Similarly, if the minimum price is set to 150,000, then the maximum price should be displayed as >=200,000 (displaying only 200,000) and so forth.

Can anyone provide guidance on how to achieve this functionality using JavaScript?

Answer №1

When you choose an item from one dropdown, it's important to identify items in the next dropdown that don't meet the corresponding condition. The structure of your dropdown menu may vary, but typically looks something like this:

Let minDD represent your Minimum Price dropdown and maxDD represent your Maximum Price dropdown.

// Adding event listeners
for(var i=0; i<minDD.children.length; i++){
    $(minDD.children[i]).click(function(){
        setBounds(maxDD, this.innerHTML, 'min');
    });
}
for(var i=0; i<maxDD.children.length; i++){
    $(maxDD.children[i]).click(function(){
        setBounds(minDD, this.innerHTML, 'max');
    });
}

// Function to filter out unwanted items

function setBounds(node, value, indicator) {
    if(indicator == 'min') {
        for(var i=0; i < node.children.length; i++) {
            if(node.children[i].innerHTML < value)
                node.removeChild(node.children[i]);
        }
    }
    if(indicator == 'max') {
        for(var i=0; i < node.children.length; i++) {
            if(node.children[i].innerHTML > value)
                node.removeChild(node.children[i]);
        }
    }
}

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

At what point should the term "function" be included in a ReactJS component?

As a beginner in ReactJS, I have been working through some tutorials and noticed that some code examples use the keyword function while others do not. This got me wondering what the difference is and when I should use each one. Render Example with functi ...

angularjs issue with $setPrestine function not providing reliable results

I have encountered an issue with the login form code. Initially, I am able to reset the form fields and error messages successfully on the first attempt (both success and failure scenarios work). However, on the second try, I am unable to reset the form fi ...

Disabling a button after clicking using jQuery

There are occasions when I can inadvertently trigger the submit button twice, resulting in the ajax being triggered twice as well. Below is my HTML code: <div class="buttons-area"> <input id="step-two" class="btn btn-primary" type="submit" v ...

Having trouble with Bootstrap 5 Carousel not sliding to the next image? Learn how to fix this issue by upgrading from Bootstrap 4 to Bootstrap 5

Having downloaded both the compiled css and js and the source files of the bootstrap 5 library, I've experimented with both. While the css seems to load fine, I'm struggling to get this example to function. It's from a bootstrap 4 related po ...

Choosing to maintain an open server connection instead of regularly requesting updates

Currently, I am in the process of developing an innovative online presentation tool. Let's dive into a hypothetical situation: Imagine one person is presenting while another connects to view this presentation. >> How can we ensure that the vie ...

Javascript does not have the capability to parse JSON

Received a JSON response from a PHP file: [ { "color": "black", "product_name": "Prod2", "revision": "apps/" }, { "color": "green", "product_name": "Prod1", "revision": "dev/" } ] (successfu ...

Unable to retrieve the saved user from the Express.js session

Below is the code in question: app.post('/api/command', function (req, res, next) { var clientCommand = req.body.command; console.log("ClientCommand: ", clientCommand); if (!req.session.step || req.session.step === EMAIL) { ...

Unlock the powers of Openlayers 5: Altering interactions and accessing targeted vertices

When making changes to a feature, there is a convenient option to delete a single vertex. The documentation explains this feature as follows: removePoint(){boolean}: Removes the vertex that is currently selected. () Working on mobile devices, I int ...

Forwarding users to a new destination through a script embedded within a frame

Currently, I am facing an issue where a page (lobby_box.php) is being loaded every 4 seconds on my main page (index.php) using JavaScript. The problem arises when the code within (lobby_box.php) that is meant to redirect the client from index.php to anothe ...

Tips for sending a JavaScript variable value to jQuery validate() submitHandler while performing an AJAX request

My validation on ajax calls is currently using a jQuery plugin. This is the current setup: $("#invoiceForm").validate({ rules: { plateNumber: { required: true, }, plateIssueState: { required: true, } }, ...

Despite adding app.use(flash()) to resolve the issue, the error 'TypeError: req.flash is not a function' persists

I encountered an error in my routes.js file: app.get('/login', function(req, res) { // Display the page and include any flash data if available res.render('login.html', { message: req.flash('loginMessage') }); }); ...

Endless Loop Issue with Google Maps API Integration in NextJS-React

Currently struggling to troubleshoot an infinite loop issue in my React function component map. I've spent a considerable amount of time analyzing the code and suspect that it might be related to the useEffects, but I'm unable to resolve it. Atta ...

JavaScript does not allow executing methods on imported arrays and maps

In my coding project, I created a map named queue in FILE 1. This map was fully built up with values and keys within FILE 1, and then exported to FILE 2 using module.exports.queue = (queue). Here is the code from FILE 1: let queue = new.Map() let key = &q ...

In Javascript, the splice method removes all elements except the specified element

I've got a straightforward script here that grabs content from an input box, converts it into an array, deletes a specific element, and then displays the remaining text. My issue is with using "splice()" to remove the item as it's deleting every ...

Error: The method 'getAjaxResponse' is not available for Object #<AbstractAjaxReq>

Looking to incorporate OOP best practices into my javascript code. Currently, I have an abstractAjaxRequest with child classes defining the getAjaxResponse function. However, I am encountering an error on the line self.getAjaxResponse(). Any ideas on how ...

The JavaScript animations in AngularJS using ng-class are not being activated

I've been attempting to apply js-defined animations to the ng-class directive using the standard syntax of add and remove, but for some reason, the animations are not running. After checking the logs, it seems that the add and remove functions are not ...

What is the method for designating the specific pages on which the stripejs script should be loaded?

The performance of the main-thread is being significantly impacted by Stripe's script, as illustrated in the image provided by Google Insights. https://i.stack.imgur.com/bmdJ2.png My concern is that the page currently experiencing issues does not ac ...

Exploring ways to leverage USB ports for data input and output within the .NET Core framework

Is there a way to communicate with USB ports in .net core 2.0? I attempted to use System.IO.Ports.SerialPort but encountered issues. When trying to retrieve ports, nothing was returned. Can you provide assistance with this issue? I am attempting to send ...

Unraveling a JavaScript object derived from an unknown class: a comprehensive guide

After finding a solution for serializing objects with known types in Javascript Serialization of Typed Objects, I now face a new challenge. I have an object of an unknown type that needs to be deserialized by code that is unaware of its specific type. The ...

Assorted presentation of list items in HTML tags

I am working on creating a poll and I was wondering if there is a way to display the questions randomly each time the page is visited. I'm thinking of storing the questions in a PHP or JavaScript array. Can anyone recommend a good tutorial that can he ...