Second Drop-down Options Based On First Drop-down August 29, 2023 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 Can I Use The Id Of An Html Element As A Variable In Javascript? Accidentally, I noticed I could use the id of an HTML eleme… How To Change Form Background Color On Focus Loss When Text Is Entered? First I would like to point out that all of this is client … Scale A Rectangle In Html And Css I want to make a rectangle (a div) with a specific scale. T… How To Align Multiple Input Using Css i'm trying to realize a simple login page and i need to… How To Make A Transition Effect Up The Input On Change I need to place this particular effect on a dropdown I need… Is Localstorage The Right Choice For This Webapp? I'm interested in building a small offline webapp and I… Reducing Table Wrapper Width For Iphone I am making small jquery app. In it i am using DataTables t… Jquery Fancybox Overlay Color I have tried to get a Fancybox iFrame to load automatically… How To Remove Gap After Footer In Bootstrap 4? Below is my html code (live demo http://...) but the footer… Render A React App In A Non React Web Page I was wondering if its possible to render an entire react a… December 2024 (1) November 2024 (40) October 2024 (62) September 2024 (18) August 2024 (359) July 2024 (331) June 2024 (681) May 2024 (1298) April 2024 (764) March 2024 (1524) February 2024 (1689) January 2024 (1375) December 2023 (1193) November 2023 (333) October 2023 (554) September 2023 (306) August 2023 (344) July 2023 (274) June 2023 (371) May 2023 (198) April 2023 (142) March 2023 (136) February 2023 (178) January 2023 (269) December 2022 (127) November 2022 (234) October 2022 (180) September 2022 (163) August 2022 (289) July 2022 (91) Menu Halaman Statis Beranda © 2022 - Html5 Cafe