I am using Ruby on Rails 3.2.2 and jquery-rails-2.0.2. In a my form I have many radio buttons and I would like to check the previous selected radio button if an user doesn’t accept a confirm message. I think I may implement something like the following
<%= form.radio_button(:name_1, 'value_1', { :onclick => ('selectRadio(this)') }) %>
<%= form.radio_button(:name_2, 'value_2', { :onclick => ('selectRadio(this)') }) %>
<%= # Other radio buttons %>
<%= form.radio_button(:name_n, 'value_n', { :onclick => ('selectRadio(this)') }) %>
<script type="text/javascript">
function selectRadio(radiobutton) {
var confirm_message = confirm('Are you sure?');
if (confirm_message) {
# Check the selected radio button.
} else {
# Re-check the previous selected radio button.
}
}
</script>
… but I have no solution to “re-check the previous selected radio button”. Is it possible? If so, how to make that?
You can use the following code block to do this
I am assuming all your considering radio buttons has a common class, like
radiosWorking Fiddle
Reference: $.change $.prop