Why Does My JQuery Filter Only Work Once On This Select Element? January 31, 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); }); Copy Now 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); Copy the 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(); Copy Solution 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; }); Copy Solution 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(){ var val = $(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(); } }); Copy http://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); }); Copy You 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 To Do Multiple Hover On List Alright so i wanted to know how i can select multiple items… WebRTC Video Constraints Not Working I'm trying to get a lower resolution from the webcam na… PHP Email & Hyperlinks I'm trying to include a hyperlink in my email but it is… How To Get Some Part Of A Html Body In Php I want to get only some lines of an HTML body and I am usin… HTML Table With Fixed Header And Fixed Leading Columns With Knockout Controls In The Header And First Column I have a HTML table which can be customized by the user: E… JQuery Change Div Background-image With FadeIn/Out I'm trying to create a fadeIn/Out effect on a site I cr… Dropdown Menu On Refresh Changes Javascript I have implemented a navbar uisng bootstrap 4,in which I ha… SEO Friendly 301 Redirect .htm To .aspx I currently have a small-midsize website with .htm extensio… How To Merge And Clean 2 Css Files With Overlapping Selectors/properties? Here I am, trying to do a decent job at maintaining a site … Error On Chartjs And Angular 5 - "can't Acquire Context From The Given Item" I need to display a graph in my Angular project. A simple d… December 2024 (1) November 2024 (40) October 2024 (60) September 2024 (23) August 2024 (378) July 2024 (343) June 2024 (695) May 2024 (1290) April 2024 (800) March 2024 (1583) February 2024 (1720) January 2024 (1400) December 2023 (1429) November 2023 (438) October 2023 (595) September 2023 (308) August 2023 (332) July 2023 (275) June 2023 (349) May 2023 (215) April 2023 (143) March 2023 (129) February 2023 (173) January 2023 (272) December 2022 (120) November 2022 (239) October 2022 (811) September 2022 (163) August 2022 (295) July 2022 (94) Menu Halaman Statis Beranda © 2022 - Html5 Library