0

I have the following working function (the lower part of it is not shown):

<script type="text/javascript">
function plan_click(clicked_id){
 let var_plan;
 let var_sgldbl;
 var_sgldbl = 'sgl';
 var_plan = clicked_id;
 document.getElementById(clicked_id).style.background = "green";
/* new code*/
 let var_tab01;
 switch (var_plan) {
 case '101':
 var_tab01 = 201;
 document.getElementById(var_tab01).style.background = "green";
 break;
}}
</script>

I want the function to be split into two functions at the /new code/ - in separate scripts as the new "tabeller" function might be placed in a js.-file. The "tabeller"-function should be called from the plan_click-function and receive the actual values from the variables: var_plan and var_sgldbl.

Which code should be inserted to make the split effective?

asked Mar 26, 2020 at 23:04
1
  • For what you're doing, consider adding a class of like "select" or something and putting the styling in the CSS. Commented Mar 26, 2020 at 23:08

1 Answer 1

1

Just move the code out to another function. var_plan should be a parameter, while var_tab01 can be the return value.

function plan_click(clicked_id) {
 let var_plan;
 let var_sgldbl;
 var_sgldbl = 'sgl';
 var_plan = clicked_id;
 document.getElementById(clicked_id).style.background = "green";
 let var_tab01 = tabeler(var_plan);
 ...
}
function tabeler(var_plan)
 let var_tab01;
 switch (var_plan) {
 case '101':
 var_tab01 = 201;
 document.getElementById(var_tab01).style.background = "green";
 break;
 ...
 }
 return var_tab01;
}
answered Mar 26, 2020 at 23:09
Sign up to request clarification or add additional context in comments.

4 Comments

@StackSlave Since most functions don't use arguments (and the introduction of default values and ... in ES6 make it less necessary) I consider that not very persuasive.
Actually, I think I misunderstood the original code, and var_tab01 doesn't need to be a parameter.
Yeah, OP probably doesn't know how to use function arguments. I just like to have that option. I also, like to use both kinds of functions. this!!!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.