Elton Santos Elton Santos - 5 months ago 6x
Ruby Question

How enable button on check the radiobutton

I have a foreach that contain 3 radio buttons, how do I enable a specific button when the radio button is checked? I have this:

// Starts by disabling all the buttons
$('.btn-portabilizar').attr('disabled', true);

// If it is checked, enable the a specific button

And my view is this:

<% @autorizacoes.each do |autorizacao| %>
<td><%= radio_button_tag 'autorizacao', autorizacao.numero_contrato %></td>
<td><%= autorizacao.numero_contrato %></td>
<input class="string required" type="text" name="portabilidade_nova_parcela" id="portabilidade_nova_parcela">
<%= link_to "Portabilizar", "#", :class => 'btn btn-primary btn-portabilizar' %>
<% end %>

Sorry for code in portuguese...


Your radio button selector is incorrect - you only attach it to radio buttons which are already checked on load, of which I'm assuming there are none. Remove the :checked part:

    $('.btn-portabilizar').prop('disabled', false);

Also note that it's better practice to use prop() where possible.

To only affect the button in the same row you would need to use jQuery's DOM traversal methods, like this:

    $(this).closest('tr').find('.btn-portabilizar').prop('disabled', false);