Steve Kim
Steve Kim

Reputation: 5591

jQuery making a function

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!

Upvotes: 1

Views: 39

Answers (1)

Phiter
Phiter

Reputation: 14992

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.

Upvotes: 2

Related Questions