0

I am trying to get the id of a radio button on click from a Rails 4 form. I am trying to use some jQuery like:

$("input:radio[name=client_account]").click(function() {
    var value = $(this).val();
    alert(value);
    var id= $(this).attr('id');
    alert(id);
});

but the "name" of the radio buttons is "client_account[client_account_attributes][org_type]"

I have an "f.collection_radio_buttons" set that is rendering in the form. I want to create a "dynamic form" that will show and hide divs based on answers to certain questions. But I will need to somehow get the id or value from the selected radio button when it is clicked so that I can determine which divs to show / hide.

Here is an example of the HTML in case it will help:

<input type="radio" value="Other" name="client_account[client_account_info_attributes][org_type]" id="client_account_client_account_info_attributes_org_type_other">

thanks in advance!!

2 Answers 2

1

I ended up doing this as my final solution

  $("input:radio[name='client_account[client_account_info_attributes][org_type]']").click(function(event) {
var value = (event.target.value); 
});
Sign up to request clarification or add additional context in comments.

Comments

0

You can quote the name of the element if it has special characters.

var $button = $("[name='client_account[client_account_info_attributes][org_type]']");
$button.click(function () {
   console.log($(this));
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.