3
\$\begingroup\$

I'm using JQVMap, and I have my code to where depending on what state you click, it'll reveal info about that state within a single div below the map. However, I feel there's a simplified, more efficient way to set up this code so there's not a lot of repeat, especially once I start adding more states. Is there?

switch(code){
    case"tx":
        $('#info-children').children(':not(#info-tx)').hide("slow");
        $('#info-children').children('#info-tx').show("slow");
        break;
    case"il":
        $('#info-children').children(':not(#info-il)').hide("slow");
        $('#info-children').children('#info-il').show("slow");
        break;
    case"fl":
        $('#info-children').children(':not(#info-fl)').hide("slow");
        $('#info-children').children('#info-fl').show("slow");
        break;
    case"ga":
        $('#info-children').children(':not(#info-ga)').hide("slow");
        $('#info-children').children('#info-ga').show("slow");
        break;
    case"pa":
        $('#info-children').children(':not(#info-pa)').hide("slow");
        $('#info-children').children('#info-pa').show("slow");
        break;
    default:
        $('#state-name').html(region);
        $('#info-children').children(':not(#info-uhoh)').hide("slow");
        $('#info-children').children('#info-uhoh').show("slow");
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

You could have a single if statement covering all your non-default cases, since all your $('info#children')... sequences are the same, except for the state clicked.

So, you could just say

$('#info-children').children(':not(#info-' + code + ')').hide('slow');

and

$('#info-children').children('#info-' + code + ')').show('slow');

So, you could just have one huge logical statement, checking if the code/state is tx, il, fl, ga, or pa. If that statement is true, run the above code; if not, run the default code.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.