Reputation: 10852
I am trying to do something so simple, and yet I can't get it to work.
radio_button_tag(:hw_choice, "Completed", onclick: {alert("I work!"); } )
I need to use radio_button_tag
, and what I want to do is to do some js any time the radio button changes. I've tried many varieties of the above, option: { on_click
:, none of them even generate the onclick
html attribute, so this should be pretty simple, html/rails helper problem.
What am I missing?
Upvotes: 2
Views: 2559
Reputation: 2812
radio_button_tag
has four parameters, the third parameter is the checked
and the fourth parameter is the HTML options you desire.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-radio_button_tag
Add a true
or false
as the third parameter. Enclose the JavaScript in quotes. Also, you don't need the parentheses around the parameters.
radio_button_tag :hw_choice, "Completed", false, onclick: "window.alert('I work!');"
Upvotes: 4
Reputation: 4825
Similar to this answer, one option is to add an event listener to the button using javascript.
button = document.getElementById("id")
button.addEventListener('click', function(){
//do stuff here
}, false);
(This solution would be implemented on the frontend rather than the backend, which may not be ideal)
Upvotes: 3