1

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
3
  • 3
    return your string, and you are done. Commented Jan 13, 2016 at 23:34
  • 3
    the string in the function should be returned Commented Jan 13, 2016 at 23:34
  • Ah,,, that was a silly mistake. Thanks for the help! Commented Jan 13, 2016 at 23:35

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

You forgot to call the function.

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.