Second Drop-down Options Based On First Drop-down April 22, 2024 Post a Comment On a web page, if you select different options on the first drop-down, different options will appear in the second drop-down. Solution 1: You could try like this.First hide all the options and show only matching options using toggleClass()$(function() { $('#independent').on('change', function (e) { $('#dependent').val(''); var endingChar = $(this).val().split('').pop(); var selected = $( '#independent' ).val(); $('#dependent option').addClass('show'); $('#dependent option[value^='+selected+']').toggleClass('show'); }) });Copy.show{ display:none; }Copy<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><selectid="independent"><optionvalue="A"> A </option><optionvalue="B"> B </option></select><selectid="dependent"><optionvalue="A021"> A1 </option><optionvalue="A22019"> A2 </option><optionvalue="A3541"> A3 </option><optionvalue="B148"> B1 </option><optionvalue="B2"> B2 </option><optionvalue="B397415"> B3 </option></select>CopySolution 2: The following loops through the options. If the option's value doesn't start with the correct first letter, a class is added to hide via css. If it does match, the class is removed. It also selects the first option that matches the correct letter, so a hidden option isn't selected.$(function() { $('#independent').on('change', function (e) { var selected = $('#independent').val().toUpperCase(); var currentDep = $('#dependent').val().charAt(0).toUpperCase(); var changedSelected = false; $('#dependent option').each(function() { var opt = $(this); var value = opt.val().charAt(0).toUpperCase(); if (value !== selected) { opt.addClass('hide'); opt.removeAttr('selected'); } else { opt.removeClass('hide'); if (!changedSelected) { opt.attr('selected', 'selected'); changedSelected = true; } else { opt.removeAttr('selected'); } } }); }); });Copy.hide { display: none; }Copy<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><selectid="independent"><optionvalue="A"> A </option><optionvalue="B"> B </option></select><selectid="dependent"><optionvalue="A021"> A1 </option><optionvalue="A22019"> A2 </option><optionvalue="A3541"> A3 </option><optionvalue="B148"> B1 </option><optionvalue="B2"> B2 </option><optionvalue="B397415"> B3 </option></select>CopySolution 3: $(function() { $('#independent').on('change', function (e) { var selected = $('#independent').val().toUpperCase(); var currentDep = $('#dependent').val().charAt(0).toUpperCase(); var changedSelected = false; $('#dependent option').each(function() { var opt = $(this); var value = opt.val().charAt(0).toUpperCase(); if (value !== selected) { opt.addClass('hide'); opt.removeAttr('selected'); } else { opt.removeClass('hide'); if (!changedSelected) { opt.attr('selected', 'selected'); changedSelected = true; } else { opt.removeAttr('selected'); } } }); }); });Copy.hide { display: none; }Copy<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><selectid="independent"><optionvalue="A"> A </option><optionvalue="B"> B </option></select><selectid="dependent"><optionvalue="A021"> A1 </option><optionvalue="A22019"> A2 </option><optionvalue="A3541"> A3 </option><optionvalue="B148"> B1 </option><optionvalue="B2"> B2 </option><optionvalue="B397415"> B3 </option></select>Copy Share Post a Comment for "Second Drop-down Options Based On First Drop-down" Top Question Input Type Datetime-local Is Not Working In Firefox I have an input type of datetime-local which is working fin… Can I Absolutely Position An Element In The Top Right Corner Of A Element? I have a table and I need the cells to have an element posi… CSS - Auto Fill Height Here's a js fiddle of what I currently have, I am tryin… Store A Generated Random Number In A Variable, Then Call That Variable As A Filename To Bring Up An Image. Javascript So I have a few pictures that I want to randomly generate o… CSS Hover Jitter Issue This is my first question. I usually try to research these … Right Floated Element Disappears When Using Columns In Firefox I am using an ol element with column-count and column-gap p… Setting Link Href From Child Image Src Using JQuery I am using .wrap() to create a link around each image withi… How To Limit The Html Table Records In Each Page I am populating a table in my jsp which has more than 20 re… Separate Countdown For Divs I want to show countdown in each of my divs. Right now I ge… Retain Dynamically Created DropDown List's Selected Value On Event Window.history.back()- JavaScript Can use the below question as a reference to explain the co… December 2024 (1) November 2024 (38) October 2024 (60) September 2024 (20) August 2024 (360) July 2024 (336) June 2024 (693) May 2024 (1285) April 2024 (802) March 2024 (1568) February 2024 (1697) January 2024 (1377) December 2023 (1447) November 2023 (380) October 2023 (599) September 2023 (335) August 2023 (347) July 2023 (287) June 2023 (341) May 2023 (234) April 2023 (140) March 2023 (130) February 2023 (178) January 2023 (281) December 2022 (110) November 2022 (237) October 2022 (815) September 2022 (159) August 2022 (296) July 2022 (96) Menu Halaman Statis Beranda © 2022 - Html5 World