I’m trying to call a Javascript function from a Ruby template, and have
zero success passing in parameters.
function myFunction(stringToDisplay) {
var my_div = document.getElementById(“my_div”);
var text = document.createTextNode("String to display is " +
stringToDisplay);
my_div.appendChild(text);
}
…
<a href="#" onclick="myFunction(’<% @hello %>’)>Call my javascript
function
If I hardcode parameters value everything works fine. However, I have no
luck passing parameters in. I’ve tried ‘<%= @hello -%>’, but it only
causes @hello to render.
I’m trying to call a Javascript function from a Ruby template, and have
zero success passing in parameters.
…
<a href="#" onclick="myFunction(’<% @hello %>’)>Call my javascript
function
If I hardcode parameters value everything works fine. However, I have no
luck passing parameters in. I’ve tried ‘<%= @hello -%>’, but it only
causes @hello to render.
I’m not clear what you want to do. What do you want to render if not
the value of @hello?
the template is rendered.
link.
I’m still not clear whether it’s just a matter of getting the
value of @hello that has been rendered into the parameter of
myFunction to appear on your page, or whether @hello must be
generated at the time the link is clicked.
If the former, and if by the looks of it the string is HTML rather
than text, you should be appending HTML to the div using
I’m still not clear whether it’s just a matter of getting the
value of @hello that has been rendered into the parameter of
myFunction to appear on your page, or whether @hello must be
generated at the time the link is clicked.
It is the former, and it works great! Thank you so much for your help!
If the former, and if by the looks of it the string is HTML rather
than text, you should be appending HTML to the div using