0

instead of using trim all the time i want use once as common for the below code... pls help me

if ("Company Profile" == $.trim(selectedValue)) {
    actionClass = "locationImportCompany.do?";
} else if ("Oppty Locations" == $.trim(selectedValue)) {
    actionClass = "locationImport.do?";
} else if ("Different Oppty" == $.trim(selectedValue)) {
    actionClass = "locationImportDiffOppty.do?";
} else if ("Loop TrackId Profile" == $.trim(selectedValue)) {
    actionClass = "locationImportLoopTrkId.do?";
} else if ("Inventory" == $.trim(selectedValue)) {
    isOrdering = "Y";
    actionClass = "inventoryLaunch.do?";
} else if ("Data Center List" == $.trim(selectedValue)) {
    actionClass = "locationImportDataCenter.do?";
} else if ("Customer AccountId" == $.trim(selectedValue)) {
    actionClass = "inventoryLaunch.do?";
} else if ("Customer Name" == $.trim(selectedValue)) {
    actionClass = "locationImportDataCenter.do?";
}
0

4 Answers 4

3

Store it in a variable:

 var selectedValue = $.trim(selectedValue);

Then do:

if (selectedValue == "Oppty Locations") {

}

etc...

1
  • ps All those if's but 1 could be replaced if a look up object is used.
    – QuentinUK
    Commented Mar 13, 2013 at 10:11
2
var selectedValue = $.trim(selectedValue);
if ("Company Profile" == selectedValue) {
    actionClass = "locationImportCompany.do?";
} else if ("Oppty Locations" == selectedValue) {
    actionClass = "locationImport.do?";
} else if ("Different Oppty" == selectedValue) {
    actionClass = "locationImportDiffOppty.do?";
} else if ("Loop TrackId Profile" == selectedValue) {
    actionClass = "locationImportLoopTrkId.do?";
} else if ("Inventory" == selectedValue) {
    isOrdering = "Y";
    actionClass = "inventoryLaunch.do?";
} else if ("Data Center List" == selectedValue) {
    actionClass = "locationImportDataCenter.do?";
} else if ("Customer AccountId" == selectedValue) {
    actionClass = "inventoryLaunch.do?";
} else if ("Customer Name" == selectedValue) {
    actionClass = "locationImportDataCenter.do?";
}
1

Over write selectedValue with trimmed value and use afterwards, or assign it to some variable and use that.

selectedValueTrimmed = $.trim(selectedValue)
if("Company Profile"==selectedValueTrimmed ){
0

Make it to Switch Statement instead of Else If Statement.

switch ($.trim(selectedValue)) {
    case: "Company Profile" 
        actionClass = "locationImportCompany.do?";
        break;
    case: "Oppty Locations" 
        actionClass = "locationImport.do?";
        break;
   // So on ...
} 

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