Second Drop-down Options Based On First Drop-down May 25, 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 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="independent"> <option value="A"> A </option> <option value="B"> B </option> </select> <select id="dependent"> <option value="A021"> A1 </option> <option value="A22019"> A2 </option> <option value="A3541"> A3 </option> <option value="B148"> B1 </option> <option value="B2"> B2 </option> <option value="B397415"> B3 </option> </select>Copy Solution 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 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="independent"> <option value="A"> A </option> <option value="B"> B </option> </select> <select id="dependent"> <option value="A021"> A1 </option> <option value="A22019"> A2 </option> <option value="A3541"> A3 </option> <option value="B148"> B1 </option> <option value="B2"> B2 </option> <option value="B397415"> B3 </option> </select>Copy Solution 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 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="independent"> <option value="A"> A </option> <option value="B"> B </option> </select> <select id="dependent"> <option value="A021"> A1 </option> <option value="A22019"> A2 </option> <option value="A3541"> A3 </option> <option value="B148"> B1 </option> <option value="B2"> B2 </option> <option value="B397415"> B3 </option> </select>Copy Share Post a Comment for "Second Drop-down Options Based On First Drop-down" Top Question Can One Use The Fetch API As A Request Interceptor? I'm trying to run some simple JS functions after every … Hide Submit Button Using Javascript How do I hide the submit button using javascript? Reason is… Why Do Browsers Allow Onmousedown Js To Change Href? I've noticed for a very long time that when you try to … C++, Win32 Api: How To Create An Html Rendering Window So That Your Application Would Get Callbacks From JS Calls? What I need is simple: we have a console app project. We wa… Menuitem And Contextmenu Crossbrowser Compatibility Problem 1: I had made my own contextmenu using the followin… Get Seleceted Dropdown Option Value Jquery I have two dropdowns -- for the month and year -- and a but… How To Escape Special Characters In Regular Expressions For my website I use the dataTable plugin and to give the u… How Can I Attach Event To A Tag Which Is In String Form? I'm creating html on runtime like this: var myVar = … AngularJS Image Upload Using Php I've got a problem with an image upload in AngularJS. I… Node.js Url Without Port Number I'm creating node.js project now. I've a VPS which … November 2024 (37) October 2024 (49) September 2024 (22) August 2024 (205) July 2024 (180) June 2024 (398) May 2024 (667) April 2024 (429) March 2024 (784) February 2024 (901) January 2024 (740) December 2023 (771) November 2023 (311) October 2023 (531) September 2023 (283) August 2023 (322) July 2023 (277) June 2023 (355) May 2023 (214) April 2023 (135) March 2023 (143) February 2023 (181) January 2023 (267) December 2022 (133) November 2022 (243) October 2022 (162) September 2022 (164) August 2022 (486) July 2022 (291) June 2022 (251) Menu Halaman Statis Beranda © 2022 - javascript dozent