Display a custom dropdown menu depending on the value chosen from a separate dropdown menu

There are three dropdown lists, each enclosed in its own DIV container.

The DIV containers are named fromCity, WithinStateLimits, and OutOfState.

The goal is to use the jQuery script below to hide the other two DIVs when a user selects an option from the dropdown list inside the fromCity DIV.

If a user selects an option from the dropdown list inside the WithinStateLimits DIV, the other two DIVs should be hidden.

Similarly, if a user selects an option from the dropdown list inside the OutOfState DIV, the other two DIVs should be hidden.

Upon page load, only the dropdown list inside the fromCity DIV should be visible by default.

Any suggestions on how to achieve this functionality?

Thank you in advance.

Re: How do I show different options based on the selected value from a dropdown list?

Just now|LINK

Thank you very much A2H,

As I am more familiar with jQuery, I attempted the code below, but it doesn't seem to work.

<script type="text/javascript">
    $('#tripType').change(function () {
        var SelectedValue = $('#<%= tripType.ClientID %> option:selected').val();

        if ((SelectedValue == 'one_way_to_airport') | (SelectedValue == 'one_way_from_airport') | (SelectedValue == 'round_trip_airport') | (SelectedValue == 'One-way trip NOT involving Airport') | (SelectedValue == 'Round trip NOT involving Airport')) {
            $('#fromCity').fadeIn();
            $('#WithinStateLimits').fadeOut();
            $('#OutOfState').fadeOut();
        }
        else if (value == 'hourly') {
            // Show or hide
            $('#fromCity').fadeOut();
            $('#OutOfState').fadeOut();
            $('#WithinStateLimits').fadeIn();
        }
        else { // value == 'Long_Distance'
            $('#fromCity').fadeOut();
            $('#WithinStateLimits').fadeOut();
            $('#OutOfState').fadeIn();
        }
    }); 
</script>

  <asp:DropdownList ID="tripType" runat="server" class="select ">
   <asp:ListItem value="">--Select One--</asp:ListItem>
   <asp:ListItem value="one_way_to_airport">One-way trip TO Airport</asp:ListItem>
   <asp:ListItem value="one_way_from_airport">One-way trip FROM Airport</asp:ListItem>
   <asp:ListItem value="round_trip_airport">Round trip involving Airport</asp:ListItem>
   <asp:ListItem value="one_way_no_airport">One-way trip NOT involving Airport</asp:ListItem>
   <asp:ListItem value="round_trip_no_airport">Round trip NOT involving Airport</asp:ListItem>
   <asp:ListItem value="hourly">Hourly/Charter</asp:ListItem>
   <asp:ListItem value="Long_Distance">Long Distance</asp:ListItem>
  </asp:DropdownList>

Answer №1

I have successfully discovered the solution, and I am sharing it here in the hope that it will benefit others.

To achieve the desired functionality of showing/hiding divs based on user selection from the tripType dropdownList, I employed a mix of CSS and JQuery.

For ensuring that only the fromCity div is displayed upon page load, I added the following style attribute to the two DIVs that should remain hidden initially:

style="display:none;"

The complete code snippet is provided below:

// JavaScript

function ShowHideDiv() {

    // Obtain the value of the selected dropdown item
    var SelectedValue = $('#<%= tripType.ClientID %> option:selected').val();

    // Check the selected value
    if ((SelectedValue == 'one_way_to_airport') || (SelectedValue == 'one_way_from_airport') || (SelectedValue == 'round_trip_airport') || (SelectedValue == 'one_way_no_airport') || (SelectedValue == 'round_trip_no_airport')) {

        // Show the fromCity div and hide the other divs if 'fromCity' is selected
        $('#fromCity').css("display", "block");
        $('#WithinStateLimits').css("display", "none");
        $('#OutOfState').css("display", "none");

    } else if (SelectedValue == 'hourly') {

        // Show the hourly div and hide the other divs if 'hourly' is selected
        $('#fromCity').css("display", "none");
        $('#WithinStateLimits').css("display", "block");
        $('#OutOfState').css("display", "none");

    } else {

        // Show the out of state div and hide the other divs if 'out of state' is selected
        $('#fromCity').css("display", "none");
        $('#WithinStateLimits').css("display", "none");
        $('#OutOfState').css("display", "block");

    }

}

// Markup:

<table>
    <tr>
        <td>
            <label for="tripType">Trip Type <abbr class="required" title="required">*</abbr></label>
            <asp:DropdownList ID="tripType" runat="server" class="select " onchange="ShowHideDiv();">
            <asp:ListItem value="">--Select One--</asp:ListItem>
            <asp:ListItem value="one_way_to_airport">One-way trip TO Airport</asp:ListItem>
            <asp:ListItem value="one_way_from_airport">One-way trip FROM Airport</asp:ListItem>
            <asp:ListItem value="round_trip_airport">Round trip involving Airport</asp:ListItem>
            <asp:ListItem value="one_way_no_airport">One-way trip NOT involving Airport</asp:ListItem>
            <asp:ListItem value="round_trip_no_airport">Round trip NOT involving Airport</asp:ListItem>
            <asp:ListItem value="hourly">Hourly/Charter</asp:ListItem>
            <asp:ListItem value="Long_Distance">Long Distance</asp:ListItem>
            </asp:DropdownList>
        </td>
        <td>
            <div id="fromCity">
                <label for="from_city_and_state">Pick off from <abbr class="required" title="required">*</abbr></label>
                <asp:DropDownList ID="from_city_and_state" runat="server" class="select " >
                <asp:ListItem value="">--Select One--</asp:ListItem>
                </asp:DropDownList>
            </div>
            <div id="WithinStateLimits" style="display:none;" runat="server">
                <label for="HourlyCharter">Hourly <abbr class="required" title="required">*</abbr></label>
                <asp:DropDownList ID="HourlyCharter" runat="server" class="select ">
                    <asp:ListItem value="3">3 Hours</asp:ListItem>
                    <asp:ListItem value="4">4 Hours</asp:ListItem>
                    <asp:ListItem value="5">5 Hours</asp:ListItem>
                    <asp:ListItem value="6">6 Hours</asp:ListItem>
                    <asp:ListItem value="7">7 Hours</asp:ListItem>
                </asp:DropDownList>
            </div>
            <div id="OutOfState" style="display:none;">
                <label for="LongDistance">Long Distance <abbr class="required" title="required">*</abbr></label>
                <asp:DropDownList ID="LongDistance" runat="server" class="select ">
                    <asp:ListItem value="2">$2 per mile</asp:ListItem>
                    <asp:ListItem value="4">$4 per mile</asp:ListItem>
                </asp:DropDownList>
            </div>
        </td>
    </tr>
</table>

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

I love the idea of the music playing on as I navigate between pages or tabs. Such a cool feature with Next

I'm looking to implement a music player within my next.js application. How can I ensure that the currently playing track doesn't stop when switching between pages? Essentially, I want the music playback to continue seamlessly as users navigate th ...

Discovered an issue with AngularJS involving ng-show and ng-if?

Upon my investigation, I have identified multiple issues with angularjs: <div ng-show="['[]']">this should be displayed but it is not working as expected</div> <div ng-show="[]">this should be displayed but it is not working as ...

Here is a step-by-step guide on creating a navigation bar with a Login and Sign Up button using Bootstrap 4

I am in the process of creating a navigation bar in bootstrap 4, which includes Login and Sign Up buttons. To get a clear idea of what I'm aiming for, please refer to the images below: https://i.sstatic.net/ibZKa.png https://i.sstatic.net/OUWsX.png ...

Redirecting the final element of the slideshow using Jquery

I am completely new to Jquery and am looking to create a manual slideshow with a page redirection when the last "next" button is clicked. My approach involves identifying the last slide element using CSS classes and then assigning a class to the next butt ...

differences in rendering of flexbox align-items: center between Chrome and Firefox

As I was attempting to center a div inside another div using flexbox, an inconsistency in rendering between Firefox and Chrome caught my attention. In the scenario, there are 3 divs, each with properties display:flex, justify-content:center, and align-ite ...

Transform the text upon hovering over the image, with the text positioned separate from the image

I'm looking for a solution where hovering over images in a grid area will change the text on the left side of the screen to display a predefined description of that image. I've been working on this problem for hours and would appreciate any help. ...

Ways to deactivate remaining buttons until a function call finishes after selecting one in a react render() function?

In order to prevent the overlap of results when multiple buttons are clicked simultaneously, I need to disable all toggle buttons until one function call is complete, including the reset button. Additionally, I'm looking for a way to display my functi ...

Maximizing the efficiency of React.js: Strategies to avoid unnecessary renders when adding a new form field on a webpage

Currently, I have a form that consists of conditionally rendered fields. These components are built using MUI components, react-hook-form, and yup for validation. In addition, within the AutocompleteCoffee, RadioBtnGroup, and TxtField components, I have i ...

Updating the size of the caret in input fields with Bootstrap 5

Is there a way to adjust the height of an input field (form-control) using Bootstrap 5? I'd love to see an example of how to make it taller or shorter - any suggestions? https://i.sstatic.net/YM1Os.png Here's a sample code snippet for reference ...

Explore the properties within an array of objects through iteration

Here is the array I'm working with: const myArray = [{ id: 1, isSet: true }, { id: 2, isSet: false }, ...]; I only need to iterate over the isSet properties of the objects, ignoring all other properties. Initially, I attempted the following solution ...

What could be causing the excessive number of entries in my mapping results?

I am in need of mapping an array consisting of dates. Each date within this array is associated with a group of dates (formatted as an array). For instance: The format array looks like this: let format = [3, 3, 1, 5, 4, 4, 3, 5, 13, 10, 3, 5, 5, 2, 2, 10] ...

Update each number in an array by appending a string within a table in an Angular component, rather than the

I have created a function that decides on a comment based on the result number added to an array and displays it in a table. However, calling this function within the template is causing a performance slowdown. Is there a way to achieve the same outcome w ...

Exploring the Contrasts in Utilizing AJAX: A Comparative Analysis of Two

Can someone explain the distinction between utilizing AJAX in the following manner, function showHint(str) { var xmlhttp; if (str.length==0) { document.getElementById("txtHint").innerHTML=""; return; } if (window.XMLHttpRequest) {// code for IE ...

JavaScript Password Form Submission - Enter your password in the form

I need assistance with setting up a password for a form. Users should be able to enter a password into a specified field, click submit, and if the password is correct, they will be redirected to buybutton.php in the same window. If the password is incorr ...

A missing semicolon is encountered while compiling a React application

I am currently working on my app using Create React App and incorporating scss. I have used a vscode plugin to convert the scss to css, but I keep encountering an error when running npm run build. The error message reads as follows: [email protected ...

Removing a row from a table seamlessly without the need to reload the page

I am having an issue with my page that displays a list of orders. When a user clicks on a button to delete a row from the table, it only removes the first row successfully. However, when attempting to delete the second or third row, it does not work. Can s ...

Finding the current div based on the scrolling position can be achieved without the use of jQuery

Seeking assistance with a coding challenge. I have a solution in jQuery, but we require the solution in plain JavaScript or Angular. It seems like something is missing. Take a look at this fiddle. The objective is to highlight the corresponding left panel ...

Best placement for transition effects in a dropdown menu animation

<div class="dropdown"> <button class="dropbtn">ALAT</button> <div class="dropdown-content"> <a href="index2.php"><img src="home.jpg" class="home" width="7 ...

Attempting to achieve a carousel animation using jQuery

Upon loading the page, only one character out of four is displayed. Two arrows are provided - one on the left and one on the right. Clicking on the left arrow causes the current character to fade out and the previous character to fade in. Clicking on the r ...

My goal is to develop a table that is both able to be resized and easily repositioned

I've been working on a project to develop an interactive table that can be manipulated, resized, and repositioned within a canvas. The code snippet below shows my attempt at creating this table on the canvas: var canvas = document.getElementById("dra ...