I have following js:
var rhp_s4_left ='<div class="col s4 rhpop_left">';
var rhp_s7_left ='<div class="col s7 rhpop_left">';
var rhp_s4_right ='<div class="col s4 rhpop_right">';
var rhp_s5_right ='<div class="col s5 rhpop_right">';
var rhp_s6_right ='<div class="col s6 rhpop_right">';
var rhp_s12_right ='<div class="col s12 rhpop_right">';
var rhp_s4_extra ='<div class="col s4 rhpop_extra">';
I want to simplify it by making a function:
function RHC(a,b){
'<div class="col s'+ a +' rhpop_'+ b +'">';
}
Then,
var rhp_s6_extra ='<div class="col s6 rhpop_extra">';
will be
var rhp_s6_extra = RHC("6","extra");
Of course this is not correct, but I am not sure how to make it work. Can someone show me ? Thanks!
asked Jan 13, 2016 at 23:31
Steve Kim
5,66117 gold badges61 silver badges100 bronze badges
-
3return your string, and you are done.Travis J– Travis J2016年01月13日 23:34:18 +00:00Commented Jan 13, 2016 at 23:34
-
3the string in the function should be returnedPhiter– Phiter2016年01月13日 23:34:31 +00:00Commented Jan 13, 2016 at 23:34
-
Ah,,, that was a silly mistake. Thanks for the help!Steve Kim– Steve Kim2016年01月13日 23:35:04 +00:00Commented Jan 13, 2016 at 23:35
1 Answer 1
Simply add a return to your function:
function RHC(a,b){
return '<div class="col s'+ a +' rhpop_'+ b +'">';
}
var rhp_s6_extra = RHC("6","extra");
In your actual code you are only adding a string into the function body. Returning it will apply the string to the calling variable.
answered Jan 13, 2016 at 23:35
Phiter
15k14 gold badges53 silver badges87 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
j08691
You forgot to call the function.
lang-js