Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7936493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:14:01+00:00 2026-06-03T22:14:01+00:00

I want to create a Javascript switch based on an array I’m creating from

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:14:03+00:00Added an answer on June 3, 2026 at 10:14 pm

    @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!” 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create array of javascript's regexp match string from the text below.
I'm not sure how I would do this in Javascript. I want to create
I want to create a string in JavaScript that contains all ascii characters. How
I want to create some DIV from javascript. I don't know how many I
Possible Duplicate: Generating a JavaScript array from a PHP array I just want to
Let's suppose I want to create a javascript class/object/function which have a method that
I want create a new JavaScript object based on an existing JavaScript object. Till
I want this javascript to create options from 12 to 100 in a select
Basically I want to create one large object of many object in JavaScript. Something
JavaScript. I want to create simple script, that will be resize loaded image using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.