The d-flex in the css has important on it so that will over-ride the display:none even when in the style attribute.
There is a way around this and you can use css to force the display back to what’s required.
Add this css after the original.
/*white space is important after display: */
#shops.d-flex[style*="display: block"],
#jobs.d-flex[style*="display: block"],
#events.d-flex[style*="display: block"] {
display: flex !important;
}
#shops.d-flex[style*="display: none"],
#jobs.d-flex[style*="display: none"],
#events.d-flex[style*="display: none"] {
display: none !important;
}
Now the js hide and show will work but it’s a little complex.
As far as the JS goes this seems to work but I’m sure @Paul_Wilkins will have better suggestions.
jQuery(document).ready(function() {
jQuery("#shops,#jobs,#events").hide();/* hide all */
jQuery("#shops").show();/* show one by default*/
jQuery(".mlduo-select").on("select2:select",
function(index) {
var selectedItem = jQuery(this).select2().val();
console.log(selectedItem);
jQuery("#shops,#jobs,#events").hide();
switch (selectedItem) {
case "event":
jQuery("#events").show();
break;
case "jobs":
jQuery("#jobs").show();
break;
case "shops-and-businesses":
jQuery("#shops").show();
break;
}
});
});
Thanks! I have added that and it now displays the first one as default and hidden the others, but doesn’t seem to change when selecting the other two items from the drop down.
I see. I have basically added that JS file by enqueuing it in WordPress. I am not sure how to enqueue it right at the bottom of the page. I’ll do some investigating.
Now that things are working, I would improve the code by updating the selection so that the same name for the same things are used.
Instead of select option of “event” and HTML element “events”, have “events” for both.
Instead of select option of “shops-and-businesses” and HTML element “shops”, use “shops” for both, or “shops-and-businesses” for both.
That way the switch statement can be removed, and the value of the select option can be used to directly control which items get shown.
Also, if the HTML sections can all be given the same classname of “panels” or some other more suitable name, then those don’t need to be separately called out in the scripting code either.
That way, the HTML code can be updated in several different ways in the future, without needing to break any of the existing JavaScript code in the process.