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?
-
For what you're doing, consider adding a class of like "select" or something and putting the styling in the CSS.Brad– Brad2020年03月26日 23:08:49 +00:00Commented Mar 26, 2020 at 23:08
1 Answer 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
Barmar
789k57 gold badges555 silver badges669 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
StackSlave
Looks okay, but it's a bad practice in JavaScript to reassign function arguments.
Barmar
@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.Barmar
Actually, I think I misunderstood the original code, and
var_tab01 doesn't need to be a parameter.StackSlave
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!!!lang-js