jquery - How refactor switch in javascript for cleaner code? -


i trying write cleaner javascript / jquery code. how refactor function cleaner , smaller. seems there better way this. works i'm sure there better way of getting writing function this. in advance!

function filterform(purpose, entry) {     switch (purpose) {         case 'business' :              switch (entry) {                 case 'single':                      $("#moreq1").css( "display", "block" );                        $("#moreq2").css( "display", "none"  );                        $("#moreq3").css( "display", "none"  );                        break;                 case 'double':                      $("#moreq1").css( "display", "block" );                        $("#moreq2").css( "display", "none"  );                        $("#moreq3").css( "display", "none"  );                        break;                 case 'multiple' :                      $("#moreq1").css( "display", "block" );                        $("#moreq2").css( "display", "block" );                        $("#moreq3").css( "display", "none"  );                        break;                 }                  break;             case 'private' :                  switch (entry) {                     case 'single'   :                          $("#moreq1").css( "display", "block" );                            $("#moreq2").css( "display", "none"  );                            $("#moreq3").css( "display", "none"  );                            break;                     case 'double'   :                          $("#moreq1").css( "display", "none"  );                            $("#moreq2").css( "display", "none"  );                            $("#moreq3").css( "display", "none"  );                            break;                     case 'multiple' :                          $("#moreq1").css( "display", "none"  );                            $("#moreq2").css( "display", "none"  );                           $("#moreq3").css( "display", "none"  );                            break;                 }                     break;             case 'tourist' :                  switch (entry) {                     case 'single'   :                          $("#moreq1").css( "display", "block" );                            $("#moreq2").css( "display", "none"  );                            $("#moreq3").css( "display", "block"  );                            break;                     case 'double'   :                          $("#moreq1").css( "display", "none" );                            $("#moreq2").css( "display", "none"  );                            $("#moreq3").css( "display", "none"  );                            break;                     case 'multiple' :                          $("#moreq1").css( "display", "none"  );                            $("#moreq2").css( "display", "none" );                            $("#moreq3").css( "display", "none" );                            break;                 }                         break;             }     } } 

while other answers have done decent job of combining possibilities, seems more logical me use "map" of possibilities, , reference that:

var filterform = (function () {     var moreq = [         "",        // can use indexes starting @ 1         "moreq1",         "moreq2",         "moreq3"     ], purposes = {         "business": {             "single": [moreq[1]],             "double": [moreq[1]],             "multiple": [moreq[1], moreq[2]]         },         "private": {             "single": [moreq[1]],             "double": [],             "multiple": []         },         "tourist": {             "single": [moreq[1], moreq[],             "double": [],             "multiple": []         }     };      return function (purpose, entry) {         var i, j, cur, display;          (i = 1, j = moreq.length; < j; i++) {             cur = moreq[i];             display = purposes[purpose][entry].indexof(cur) > -1 ? "block" : "none";             console.log("setting " + cur + " " + display);             //$("#" + cur).css("display", display);         }     }; }());  filterform("business", "multiple"); 

demo: http://jsfiddle.net/w3jnn/1/

the arrays in object elements set display block. want change specific setting, modify purposes object's arrays.

and of course, function doesn't checking purposes[purpose][entry].indexof(cur) > -1 part - assumes purpose , entry parameters have valid values. modified check, didn't seem important right now.

as note, uses closure (the surrounding (function () { })) instantiate purposes , items variables once, yet keep them out of global scope.

also note, array.indexof method isn't supported in browsers (mainly old ie), there's shim @ https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/array/indexof#compatibility include on page make sure works, among other ones can find google.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -