Question

I want to add a class to each radio field in my form, when it is selected. I followed the steps for documentation for custom inputs, but it didn't work. Do I just need to do this with JS?

What I tried:

# app/inputs/collection_select_input.rb
class CollectionSelectInput < SimpleForm::Inputs::CollectionSelectInput
  def input_html_classes
    super.push('chosen')
  end
end
Was it helpful?

Solution

Nope, you can't use pure ruby to do this. Ruby on Rails is a server side language, and for this kind of job, you can't use it, you should definitely use Javascript.

Using Jquery for instance, assuming your radio field got the radio class, and you wish it to get the newclass class on clicked:

$("input[type='radio']").click(function() {
  $(this).addClass('newclass');
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top