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 8674381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:41:50+00:00 2026-06-12T19:41:50+00:00

I am using twitter bootstrap in my rails app, and I am constructing a

  • 0

I am using twitter bootstrap in my rails app, and I am constructing a form with multiple radio button inputs. In order to manage the form data, I have created a hidden field whose value is updated via Javascript.

Sample code below for any given input:

<div class="btn-group" data-toggle="buttons-radio">
          <button type="button" class="btn" id="ornamental_only_yes" name="ornamental_only" value="yes">Yes</button>
          <button type="button" class="btn" id="ornamental_only_no" name="ornamental_only" value="no">No</button>
        </div>
        <input type="hidden" id="ornamental_only" name="ornamental_only" value="">

Here is the Javascript to manage this event:

<script>
  var btns = ['ornamental_only_yes', 'ornamental_only_no'];
  var input = document.getElementById('ornamental_only');
  for(var i = 0; i < btns.length; i++) {
    document.getElementById(btns[i]).addEventListener('click', function() {
      input.value = this.value;
    });
  }
</script>

My question: Is there a way to abstract the Javascript code in such a way as to apply to multiple radio button inputs simultaneously? (For instance, if I had 3 repetitions of the code block above with different id’s)

  • 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-12T19:41:51+00:00Added an answer on June 12, 2026 at 7:41 pm

    Let’s assume this is your HTML with multiple sets of buttons:

    <div class="btn-group" id="btn_group_1" data-toggle="buttons-radio">
        <button type="button" class="btn" id="ornamental_only_yes" name="ornamental_only" value="yes">Yes</button>
        <button type="button" class="btn" id="ornamental_only_no" name="ornamental_only" value="no">No</button>
    </div>
    <input type="hidden" id="hidden_1" name="ornamental_only" value="">
    <div class="btn-group" id="btn_group_2" data-toggle="buttons-radio">
        <button type="button" class="btn" id="awesome_only_yes" name="awesome_only" value="yes">Yes</button>
        <button type="button" class="btn" id="awesome_only_no" name="awesome_only" value="no">No</button>
    </div>
    <input type="hidden" id="hidden_2" name="awesome_only" value="">
    

    To dynamically listen for clicks on all button groups, you could do something like this (untested jQuery):

    $('.btn-group .btn').on('click', function() {
        var parentID = $(this).parent().attr('id');
        $('#hidden_' + parentID.substr(parentID.length - 1)).val($(this).val());
    });
    

    This just targets the hidden input fields based on the number at the end of the id.

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

Sidebar

Related Questions

I have deployed a rails app on Heroku, and I am using the twitter-bootstrap-rails
I have a rails 3.2 app using Twitter Bootstrap via the gem ' twitter-bootstrap-rails
I am using Twitter Bootstrap in my rails app. My navbar looks perfect in
I'm asking about using a Twitter Bootstrap modal window with a Rails form helper
Using Twitter Bootstrap's bootstrap-tab.js, I have: <ul class=tabnavcenter id=myTab> <li class=active><a href=#home data-toggle=tab>about</a></li> <li><a
I am using Twitter bootstrap for Rails/SASS and have notice that it applies a
Going nuts here. I'm developing a rails app, and I'm using the twitter-bootstrap-rails gem
In my rails app, I'm using Twitter bootstrap. Before, everything was working fine, but
I am working on a really simple app that is styled using twitter-bootstrap-rails. One
I have a Rails app with Twitter Bootstrap installed. Bootstrap encapsulates each navigation bar

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.