Build a switch based on array
arrays, javascript, jquery, switch-statement
Solution
@Michael has the correct general answer, but here's a far simpler way to accomplish the same goal:
// Once, at startup
var $items = $("#general,#controlpanel,#database");
// When it's time to show a target
$items.hide(); // Hide 'em all, even the one to show
$(target).show(); // OK, now show just that one
If you really only have an array of selectors then you can create a jQuery collection of them via:
var items = ["#general","#controlpanel","#database"];
var $items = $(items.join(','));
Oh, and "Thanks, Alot!" :)
Problem
I want to create a Javascript switch based on an array I'm creating from a query string. I'm not sure how to proceed. Let's say I have an array like this : ``` var myArray = ("#general","#controlpanel","#database"); ``` I want to create this... ``` switch(target){ case "#general": $("#general").show(); $("#controlpanel, #database").hide(); break; case "#controlpanel": $("#controlpanel").show(); $("#general, #database").hide(); break; case "#database": $("#database").show(); $("#general, #controlpanel").hide(); break; } ``` myArray could contain any amount of elements so I want the switch to be created dynamically based on length of the array. The `default` case would always be the first option. The array is created from a location.href with a regex to extract only what I need. Thanks alot!