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

  • Home
  • SEARCH
  • 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 9088577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:56:01+00:00 2026-06-16T21:56:01+00:00

I have a form that has many select inputs that are grouped by theme.

  • 0

I have a form that has many select inputs that are grouped by theme. I have a master select for each group that allows a user to set all selects in a group to the master value (gradescales is a method that provides grades 1-8).

How could I go about DRYing this up?

FORM

<label>Change all to:</label>
<%= select_tag "section_one", options_for_select(gradescales), :id => "section_one_master" %>
    
    <div id="section_one_wrapper">
      <%= f.input :item_one,   :collection => gradescales %>
      <%= f.input :item_two,   :collection => gradescales %>
      <%= f.input :item_three, :collection => gradescales %>
      <%= f.input :item_four,  :collection => gradescales %>
    </div>

jquery functions look like:

$("#section_one_master").change( function() { 
   var a = $("#section_one_master").val(); 
   $("#section_one_wrapper select").val(a);
 });

$("#section_two_master").change( function() { 
   var a = $("#section_two_master").val(); 
   $("#section_two_wrapper select").val(a);
 });    

 ...again, and again

So, when section_one_master is changed, the function is hit and all the select inputs inside the section_one_wrapper div are changed to match whatever value was chosen in the master.

I have many groups, each with a master select input. How could I DRY this up to one function, and then dynamically id the master that called the function in the first place and use convention to change the appropriate grouped selected?

  • 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-16T21:56:02+00:00Added an answer on June 16, 2026 at 9:56 pm

    You can add classes to your master section elements:

    $(".section-masters").change( function() {  
        var id = this.id.replace('master', 'wrapper');   
        $('#' + id + " select").val(this.value);
    });
    

    Note that in the context of change handler this refers to the element, you don’t have to reselect the element.

    You can also select the section wrapper elements and cache the object and use filter method, by caching the object, you only select those elements once, which is better than re-selecting them.

    var $wrappers = $('.section-wrappers');
    //...
    $wrappers.filter('[id="' + id + '"]').val(this.value);  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a form that has a multiple select drop down. a user can
I have a form that has many long-methods. My quest is: What is the
I have a workbook that has many checkboxes (form control) on one tab. I
I have a form that has a group of 3 text_fields. I need two
I have a form that has 90 buttons on it, one button for each
I have a user table that has many columns, it looks roughly like this:
I have a page with many forms on it. Each form has a unique
I have form partial that allows the user to enter the subject and message
I have a form that allows the user to add or remove users from
I have a form that has label values that I would like to pass

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.