Why Does My Jquery Filter Only Work Once On This Select Element? September 05, 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 Getting The Value Of A Combo-box Using Post Method I wanted to use php to get the value of a combo box, and I … HTML5 Image Drag Event - CtrlKey Remains False In Firefox 29 This works great in Chrome, but what do I need to change to… Firefox, Chrome, Safari Have Grey Background For MP4 HTML5 Video Any video (that I can make) with a white background becomes… Is There Any Way To Colorize Only Part Of Image On Hover? I would love to code simple image painting in HTML, CSS and… How To Force The Input Date Format To Dd/mm/yyyy? I have a little problem. I am developing a web system and t… Attach An Onload Handler On A Window Opened By Javascript I want to be able to know when a user navigates away from t… How Do I Change The "actual Encoding" Of My HTML Document? I ran my web page through the W3C HTML validator and receiv… Spin Wheel Image In HTML5 (e.g., Roulette Wheel)? What's the best way to emulate a spinning roulette whee… Line Break Html Email new to the forum and trying my hand at some coding to help … Javascript - Append Text After URL Element Is there a way to append something to a URL using javascrip… December 2024 (1) November 2024 (39) October 2024 (52) September 2024 (19) August 2024 (203) July 2024 (187) June 2024 (413) May 2024 (672) April 2024 (459) March 2024 (817) February 2024 (886) January 2024 (828) December 2023 (858) November 2023 (382) October 2023 (608) September 2023 (294) August 2023 (328) July 2023 (285) June 2023 (350) May 2023 (216) April 2023 (145) March 2023 (131) February 2023 (172) January 2023 (263) December 2022 (132) November 2022 (221) October 2022 (178) September 2022 (164) August 2022 (285) July 2022 (99) Menu Halaman Statis Beranda © 2022 - Html5 College