Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8643857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:09:17+00:00 2026-06-12T12:09:17+00:00

I need a solution to hide/show a checkbox depending a radio button selection. I

  • 0

I need a solution to hide/show a checkbox depending a radio button selection. I tried different solution found here and there but I can make them work.

CONTEXT:
I have a pool of contact persons that can be associated to a business. A user can select multiple contact persons but MUST select a primary contact person. So I have a radio button group whit all the contact persons to select the primary contact, and I have a checkbox group with again all the contact persons to select the “other” contacts.

NEED:
Now, what I like to do is that when the user select the primary contact in the radio button group, this contact will be removed from the “other contact” checkbox group, so the user cannot select the same contact twice. I can use JQuery as well as plain JavaScript.

CONSTRAINT:
The radio button group, as well as the checkbox group, are dynamically created using Rails Tag helper. So it’s kind of tricky for the ids and name of the elements. Also, there is a has_many :trough between business and contact.

So for the radio button, I have:

<% @contacts.each do |contact| %>
<li><%= radio_button_tag 'primary_contact', contact.id, @primary_contact == contact, :class => 'radio'%>&nbsp;&nbsp;<%= contact.name %>,&nbsp;<%= contact.contact_type.name %></li>
<% end %>

And for the checkbox group, I have:

<%= hidden_field_tag 'business[contact_ids][]', "" %>
<% @contacts.each do |contact| %>
<li><%= check_box_tag 'business[contact_ids][]', contact.id, @business.contacts.include?(contact), :class => 'radio' %>&nbsp;&nbsp;<%= contact.name %>,&nbsp;<%= contact.contact_type.name %></li>
<% end %>

Any suggestion about other design (using radio buttons and checkboxes…) to achieve my goal are very welcome.

Thanks
Alain

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:09:18+00:00Added an answer on June 12, 2026 at 12:09 pm
    <li><%= radio_button_tag ... :id => "#{contact.id}", :onclick => 'removeFromOtherList(this.id);' %><%= "  #{contact.name}, #{contact.contact_type.name}" %></li>
    

    This way you have the contact ids for the HTML tag id. Onclick it will remove\disable the other contact

    Do the same for the other contacts

    ...<%= check_box_tag ... :class => 'radio', :id => "otherContact#{contact.id}" %> ...
    

    Then we can remvoe the correct contact from the other list

    function removeFromOtherList(contactId) {
       $("#otherContact"+contactId).parent().remove(); //to remove the LI
    }
    

    Demo

    Edit:

    One way to do it is use show/hide and keep track of the last selected radio

    var lastSelectedRadioId = null;
    function removeFromOtherList(contactId) {
      $("#otherContact"+contactId).parent().hide();
      if(lastSelectedRadioId ){
         $("#otherContact"+lastSelectedRadioId ).parent().show();       
      }
      lastSelectedRadioId = contactId;
    }
    

    Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a solution for the really common but important issue that i am
I'm working on a website where I need to show/hide table rows. I got
How to show/hide big image by clicking on thumbnails? I need like this Try
In Perl Win32::GUI desktop apps, While checkbox checked status I need to hide some
I need solution of auto hide/remove table-rows of deleted records. Suppose a table-body element
I need to hide or show a div that have a slideshow inside. The
This may be a simple C# question but I need a solution. I have
I need to be able to hide/show multiple div tags on the same page
I need a solution for a conditional WHERE clause in SQL Server. I can't
I need a solution to embed a pdf inside a flash screen i.e. want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.