{"id":6459,"date":"2020-05-20T13:27:12","date_gmt":"2020-05-20T18:27:12","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=6459"},"modified":"2021-05-06T09:48:19","modified_gmt":"2021-05-06T14:48:19","slug":"filtering-html-drop-down","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=6459","title":{"rendered":"Filtering HTML Drop-down"},"content":{"rendered":"\n<p>I\u2019ve got a few drop-downs that I\u2019ve added filtering on the drop-down \u2013 start typing and you\u2019ll see the options that match your string. But I needed to mirror an application functionality where you select a category and are then presented with a list of options that fit the category.<\/p>\n\n\n\n<p>Here&#8217;s the drop-down selector for the categories<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n    echo &quot;      &lt;div class=\\&quot;row\\&quot;&gt;\\n&quot;;\n    echo &quot;          &lt;div class=\\&quot;col-md-12 col-sm-12 col-lg-12 col-xs-12\\&quot;&gt;\\n&quot;;\n    echo &quot;              &lt;div class=\\&quot;row\\&quot;&gt;\\n&quot;;\n    echo &quot;                  &lt;div class=\\&quot;row\\&quot;&gt;\\n&quot;;\n    echo &quot;                      &lt;div class=\\&quot;col-md-2 col-sm-2 col-lg-2 col-xs-2 text-left\\&quot;&gt;\\n&quot;;\n    echo &quot;                          &lt;span&gt;&lt;strong&gt;Animal Category:&lt;\/strong&gt;&lt;\/span&gt;\\n&quot;;\n    echo &quot;                      &lt;\/div&gt;\\n&quot;;\n    echo &quot;                      &lt;div class=\\&quot;col-md-10 col-sm-10 col-lg-10 col-xs-10 text-left form-group\\&quot;&gt;\\n&quot;;\n    echo &quot;                          &lt;select name=\\&quot;strAnimalType\\&quot; id=\\&quot;strAnimalType\\&quot; readonly\/&gt;\u00a0\\n&quot;;\n    echo &quot;                              &lt;option class=\\&quot;NoSelection\\&quot; value=\\&quot;-----\\&quot;&gt;-----&lt;\/option&gt;\\n&quot;;\n    echo &quot;                              &lt;option class=\\&quot;Feline\\&quot; value=\\&quot;201\\&quot;&gt;Feline&lt;\/option&gt;\\n&quot;;\n    echo &quot;                              &lt;option class=\\&quot;Canine\\&quot; value=\\&quot;202\\&quot;&gt;Canine&lt;\/option&gt;\\n&quot;;\n    echo &quot;                              &lt;option class=\\&quot;Equine\\&quot; value=\\&quot;203\\&quot;&gt;Equine&lt;\/option&gt;\\n&quot;;\n    echo &quot;                              &lt;option class=\\&quot;Other\\&quot; value=\\&quot;204\\&quot;&gt;Other&lt;\/option&gt;\\n&quot;;\n    echo &quot;                          &lt;\/select&gt;\\n&quot;;\n    echo &quot;                      &lt;\/div&gt;\\n&quot;;\n    echo &quot;                  &lt;\/div&gt;\\n&quot;;\n<\/pre><\/div>\n\n\n<p>And here&#8217;s the drop-down selector I want to filter based on category &#8212; there are a <em>lot<\/em> of options. The class for each option includes the category selectors that will include the option in the drop-down. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n    echo &quot;      &lt;div class=\\&quot;row\\&quot;&gt;\\n&quot;;\n    echo &quot;          &lt;div class=\\&quot;col-md-12 col-sm-12 col-lg-12 col-xs-12\\&quot;&gt;\\n&quot;;\n    echo &quot;              &lt;div class=\\&quot;row\\&quot;&gt;\\n&quot;;\n    echo &quot;                  &lt;div class=\\&quot;row\\&quot;&gt;\\n&quot;;\n    echo &quot;                      &lt;div class=\\&quot;col-md-2 col-sm-2 col-lg-2 col-xs-2 text-left\\&quot;&gt;\\n&quot;;\n    echo &quot;                          &lt;span&gt;&lt;strong&gt;Pet Breed:&lt;\/strong&gt;&lt;\/span&gt;\\n&quot;;\n    echo &quot;                      &lt;\/div&gt;\\n&quot;;\n    echo &quot;                      &lt;div class=\\&quot;col-md-10 col-sm-10 col-lg-10 col-xs-10 text-left form-group\\&quot;&gt;\\n&quot;;\n    echo &quot;                          &lt;select name=\\&quot;strPetBreed\\&quot; id=\\&quot;strPetBreed\\&quot; readonly\/&gt;\u00a0\\n&quot;;\n    echo &quot; &lt;option value=\\&quot;-----\\&quot; class=\\&quot;selectors All\\&quot;&gt;-----&lt;\/option&gt;\\n&quot;;\n    echo &quot; &lt;option value=\\&quot;101\\&quot; class=\\&quot;selectors Feline\\&quot;&gt;Domestic Shorthair&lt;\/option&gt;\\n&quot;;\n    echo &quot; &lt;option value=\\&quot;1275\\&quot; class=\\&quot;selectors Feline\\&quot;&gt;Flame Point Siamese&lt;\/option&gt;\\n&quot;;\n    echo &quot; &lt;option value=\\&quot;1069\\&quot; class=\\&quot;selectors Equine\\&quot;&gt;Arabian&lt;\/option&gt;\\n&quot;;\n    echo &quot; &lt;option value=\\&quot;1071\\&quot; class=\\&quot;selectors Equine\\&quot;&gt;Tennessee Walking Horse&lt;\/option&gt;\\n&quot;;\n    echo &quot; &lt;option value=\\&quot;1072\\&quot; class=\\&quot;selectors Other\\&quot;&gt;Chicken&lt;\/option&gt;\\n&quot;;\n    echo &quot; &lt;option value=\\&quot;1073\\&quot; class=\\&quot;selectors Other\\&quot;&gt;Snake&lt;\/option&gt;\\n&quot;;\n    echo &quot; &lt;option value=\\&quot;1074\\&quot; class=\\&quot;selectors Canine\\&quot;&gt;Australian Shepherd&lt;\/option&gt;\\n&quot;;\n    echo &quot; &lt;option value=\\&quot;1075\\&quot; class=\\&quot;selectors Feline\\&quot;&gt;Burmese&lt;\/option&gt;\\n&quot;;\n    echo &quot; &lt;option value=\\&quot;1076\\&quot; class=\\&quot;selectors Canine\\&quot;&gt;Siberian Husky&lt;\/option&gt;\\n&quot;;\n    echo &quot; &lt;option value=\\&quot;1077\\&quot; class=\\&quot;selectors Feline\\&quot;&gt;Sphinx&lt;\/option&gt;\\n&quot;;\n    echo &quot; &lt;option value=\\&quot;1078\\&quot; class=\\&quot;selectors Other\\&quot;&gt;Rabbit&lt;\/option&gt;\\n&quot;;\n    echo &quot;                          &lt;\/select&gt;\\n&quot;;\n    echo &quot;                      &lt;\/div&gt;\\n&quot;;\n    echo &quot;                  &lt;\/div&gt;\\n&quot;;\n    echo &quot;              &lt;\/div&gt;\\n&quot;;\n    echo &quot;          &lt;\/div&gt;\\n&quot;;\n    echo &quot;      &lt;\/div&gt;\\n&quot;;\n\n<\/pre><\/div>\n\n\n<p>In the JavaScript, I&#8217;ve got a quick function that repopulates the rather long drop-down menu based on the selected category<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\/\/ Filter strPetBreed options based on strAnimalCategory value\n$(document).ready(function () {    \n    var allOptions = $(&#039;#strPetBreedoption&#039;)\n    $(&#039;#strAnimalCategory&#039;).change(function () {\n        $(&#039;#strPetBreed option&#039;).remove()\n\n        var classN = $(&#039;#strAnimalCategory option:selected&#039;).prop(&#039;class&#039;);\n        var optsCat = allOptions.filter(&#039;.&#039; + classN);\n        $.each(optsCat, function (i, j) {\n            $(j).appendTo(&#039;#strPetBreed&#039;);\n        });\n\n        var optsAll = allOptions.filter(&#039;.All&#039;);\n        $.each(optsAll, function (i, j) {\n            $(j).prependTo(&#039;#strPetBreed&#039;);\n        });\n\n    });\n});\n\n<\/pre><\/div>\n\n\n<p>Since it\u2019s possible there are options you\u2019d want to <em>always<\/em> appear (in my case, it\u2019s just the \u201c&#8212;&#8211;\u201d to indicate no selection has been made \u2026 but there could be real items that fall into each category too), I\u2019ve got an \u201cAll\u201d classification that will get popped onto the top of the list.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019ve got a few drop-downs that I\u2019ve added filtering on the drop-down \u2013 start typing and you\u2019ll see the options that match your string. But I needed to mirror an application functionality where you select a category and are then presented with a list of options that fit the category. Here&#8217;s the drop-down selector for &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[45,765,856,854],"class_list":["post-6459","post","type-post","status-publish","format-standard","hentry","category-coding","tag-coding","tag-html","tag-javascript","tag-jquery"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/6459","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6459"}],"version-history":[{"count":7,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/6459\/revisions"}],"predecessor-version":[{"id":7779,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/6459\/revisions\/7779"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}