Reputation: 23
Edit: a, b, c are objects.
I have an erb file as follows with a, b, c variables.
<script type="text/javascript">
function fun(a,b,c){
console.log(a);
console.log(b);
console.log(c);
}
</script
<% a %>
<% b %>
<% c %>
<input type="checkbox" id="group_box" onclick="fun(a,b,c);"/>
But either the variable gets passed as string or it throws Invalid or Unexpected token error.
What am I doing wrong?
This is all I have tried so far -
onclick = "fun(a, b, c)"
onclick = "fun('a, b, c')"
onclick = "fun(\'' + a + '\', \'' + b + '\', \'' + c + '\')"
onclick = "fun(\'' + <%= a %> + '\', \'' + <%= b %> + '\', \'' + <%= c %> + '\')"
TIA
Upvotes: 0
Views: 691
Reputation: 31
you must first wrap the whole string function using erb then do interpolation to call those variables.
<% a = 1 %>
<% b = 2 %>
<% c = 3 %>
<input type="checkbox" id="group_box" onclick="<%= "fun(#{a},#{b},#{c});" %>"/>
the result will be this
<input type="checkbox" id="group_box" onclick="fun(1,2,3);"/>
if your a,b,c
variables are Hash and you want to pass object to fun
function, then you need to convert it to json first.
<% a = a.to_json.html_safe %>
<% b = b.to_json.html_safe %>
<% c = c.to_json.html_safe %>
<input type="checkbox" id="group_box" onclick="<%= "fun(#{a},#{b},#{c});" %>"/>
Upvotes: 1