Why Does My Jquery Filter Only Work Once On This Select Element? April 06, 2024 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 How Do You Make An Html Page Fade Out While Another Fades In? I have a client with an event planning site asking his page… Get MediaStreamTrack(audio) From Video I want to record audio from video element alongside recordi… Building A Wysiwyg Editor I need to build a wysiwyg editor for a project I am working… JQuery Ul Li Hover Effect I have this code here and I am trying to show the nested .s… Buttons Are Not Appearing At Top Right I just want to show my buttons at top right corner of the s… Content Container To Fit Screensize? If got a very basic layout, with a header, content containe… Css Box Model Does Not Stretch Using Padding I have a project title as a header in my webpage. This proj… Php Form - Undefined Constant ’php_self’ I have a contact form, it works fine when hosted on my serv… How To Limit The HTML Table Records In Each Page I am populating a table in my jsp which has more than 20 re… How To Limit The Html Table Records In Each Page I am populating a table in my jsp which has more than 20 re… December 2024 (1) November 2024 (38) October 2024 (67) September 2024 (19) August 2024 (366) July 2024 (342) June 2024 (686) May 2024 (1293) April 2024 (800) March 2024 (1545) February 2024 (1711) January 2024 (1405) December 2023 (1445) November 2023 (514) October 2023 (562) September 2023 (297) August 2023 (330) July 2023 (286) June 2023 (341) May 2023 (215) April 2023 (143) March 2023 (140) February 2023 (168) January 2023 (251) December 2022 (118) November 2022 (209) October 2022 (301) September 2022 (165) August 2022 (304) July 2022 (83) Menu Halaman Statis Beranda © 2022 - Html5 Dictionary