Why Does My Jquery Filter Only Work Once On This Select Element? October 10, 2023 Post a Comment I'm trying to filter results based on what a user selects in a select form. Here is my HTML: Colour Options ); $("#product-multiple-1").change(function(){ var matches = $options.filter('.'+$(this).val()).clone(); $("#product-multiple-2").html(matches); }); CopyNow you can go back to the well as often as needed . Trying to hide options does not work in IE Solution 2: you are removing all elements that have a different class than the selected options value:$("#product-multiple-2").empty().html(matches); Copythe line is effectively removing all options first, then adds the ones that matched on the line before.you could instead hide everything: (approximately)$("#product-multiple-2 option").hide(); matches.show(); CopySolution 3: When you do the first empty(), you don't have any other options any more, only the red ones. So, for example, when you run it for the first time, selecting Red on select1, you get only the red options in select 2 - FOREVER! When you run the second time, Green for example, there are no Green options in select2, so you get an empty nodelist. you should use jQuery's hide() and show(), like this: $("#product-multiple-1").change(function(){ var matches = $("#product-multiple-2 option." + $(this).val()); $("#product-multiple-2 option").not('.'+ $(this).val()).css('display', 'none'); matches.css('display', 'block')) return matches; }); CopySolution 4: It's because you are removing the items that don't match so when you select a different colour, the items for that colour are gone.Save the originals and reload them instead like this:var productMultiple2 = $("#product-multiple-2"); productMultiple2.data('originalOptions', productMultiple2.html()); $("#product-multiple-1").change(function(){ varval = $(this).val(); productMultiple2.html(productMultiple2.data('originalOptions')); if (val !== "Choose Option") { $("#product-multiple-2 > option").filter(function() { return !$(this).is("." + val) && $(this).val() !== "Choose Option"; }).remove(); } }); Copyhttp://jsfiddle.net/uazKL/5/Fixed to not break with "Choose Option"Solution 5: The reason this is happening is because you're removing all the options from the second select tag in order to display the ones with a class that matches the first select's value.There's two (maybe more?) things you can do:You can keep a copy of the option elements in a variable and then grab the elements you want like so:var $options = $("#product-multiple-2 > option"); $("#product-multiple-1").change(function(){ var $matches = $options.filter("." + $(this).val()); $("#product-multiple-2").empty().html($matches); console.log($matches); }); CopyYou can disable the options that are not available in the second select like so:$("#product-multiple-1").change(function(){ $("#product-multiple-2 > option").each(function(index, elem) { if ($(elem).is("." + $(this).val())) { elem.disabled = false; } else { elem.disabled = true; } }); console.log($matches); }); Copy Share Post a Comment for "Why Does My Jquery Filter Only Work Once On This Select Element?" Top Question Secure Way Of Passing Values Using Post Method I need to pass a few values using POST method form to anoth… Border Issue In Floating Div Further to the discussion in Floating div issue in IE i add… Red Border Still Appears On Inputs After Resetting An Html 5 Form Firefox I have an HTML 5 form! Great, I'm excited! I decide to … Tooltip(title Attribute) Is Getting Truncated Scenario: I am working on a product where i need to display… How To Hide And Show A Table-row (css-tables) I'm trying to hide and show a table row using css-table… SVG Animation And Firefox I'm struggle with SVG animation drawing, which is work … Dropdown Menu On Refresh Changes Javascript I have implemented a navbar uisng bootstrap 4,in which I ha… Flex-box Only Display As Many Items That Fit In A Container I want to only use CSS to make it so I have a container whi… How To Prevent Div From Getting Cut Of When Resizing Window? I have a div on the left side of the browser that shows fin… Html5 Multiple Canvases Event Listener - How To Determine Which Canvas Experienced The Event? I'm new to JS. I'm not using jQuery, and I have a q… December 2024 (1) November 2024 (39) October 2024 (56) September 2024 (18) August 2024 (192) July 2024 (178) June 2024 (395) May 2024 (700) April 2024 (462) March 2024 (878) February 2024 (949) January 2024 (810) December 2023 (851) November 2023 (425) October 2023 (657) September 2023 (299) August 2023 (328) July 2023 (278) June 2023 (340) May 2023 (206) April 2023 (151) March 2023 (138) February 2023 (178) January 2023 (241) December 2022 (132) November 2022 (226) October 2022 (179) September 2022 (158) August 2022 (296) July 2022 (90) Menu Halaman Statis Beranda © 2022 - Html5 Developer