Why Does My Jquery Filter Only Work Once On This Select Element? May 24, 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 SweetAlert - Change Modal Width? I love this library. I'd like to use it to display a m… Iframe Body Remove Space My iframe a style of style='width:100%', and it alm… Javascript - Append Text After Url Element Is there a way to append something to a URL using javascrip… How To Access File From An External Folder In Flask Server? I am new to flask and am very confused regarding including … Issue When Rendering A Torus In WebGL I'm writing a program that is supposed to draw 3D param… Beautifulsoup And My Code: html = ' ' from bs4 import BeautifulSoup… HTML5 Request Response Webservice I am pretty new to html5 web development I have created a p… Why Javascript Date.setdate() Change The Value Of Other Date Variables When i'm trying to know functionality of setDate() ,set… Alternative Of 'getelementbyid' In Angularjs Check this PLNKR, i have one list with id myMenuList, this … Add A Span Tag Inside P After Certain Amount Of Characters Suppose you have this for your HTML: I have some content th… December 2024 (1) November 2024 (38) October 2024 (60) September 2024 (15) August 2024 (362) July 2024 (329) June 2024 (679) May 2024 (1295) April 2024 (750) March 2024 (1520) February 2024 (1668) January 2024 (1353) December 2023 (1223) November 2023 (231) October 2023 (483) September 2023 (269) August 2023 (341) July 2023 (275) June 2023 (354) May 2023 (189) April 2023 (141) March 2023 (139) February 2023 (178) January 2023 (261) December 2022 (134) November 2022 (161) October 2022 (172) September 2022 (162) August 2022 (287) July 2022 (94) Menu Halaman Statis Beranda © 2022 - Free Interactive Html5 Tutorial