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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:00:47+00:00 2026-06-09T23:00:47+00:00

I am making checkboxes/radio/toggle groups for Bootstrap, but I failed with checkboxes – can

  • 0

I am making checkboxes/radio/toggle groups for Bootstrap, but I failed with checkboxes – can you help me to make them work?

HTML:

<form id="edit-accounts" class="form-horizontal" method="post">
    <fieldset>

        <div class="control-group">
            <label class="control-label">Radio Group:</label>
            <div class="controls btn-group" data-toggle="buttons-radio" data-toggle-name="radio_group">
                <button type="button" value="1" class="btn" data-toggle="button">first</button>
                <button type="button" value="2" class="btn" data-toggle="button">second</button>
                <button type="button" value="3" class="btn" data-toggle="button">third</button>
            </div>
            <input type="hidden" name="radio_group" value="1" />
        </div>

        <div class="control-group">
            <label class="control-label">Checkbox Group:</label>
            <div class="controls btn-group" data-toggle="buttons-checkbox" data-toggle-name="checkbox_group">
                <button type="button" value="1" class="btn" data-toggle="button">js</button>
                <button type="button" value="2" class="btn" data-toggle="button">fiddle</button>
                <button type="button" value="3" class="btn" data-toggle="button">alpha</button>
            </div>
            <input type="hidden" name="checkbox_group" value="1" />
        </div>

        <div class="control-group">
            <label class="control-label">Toggle:</label>
            <div class="controls btn-group" data-toggle="button" data-toggle-name="button_group">
                <button type="button" value="1" class="btn" data-toggle="button">on/off</button>
            </div>
            <input type="hidden" name="button_group" value="1" />
        </div>
    </fieldset>
</form>​

JavaScript:

$(function () {
    $('div.btn-group[data-toggle-name=*]').each(function () {
        var form = $(this).parents('form').eq(0);
        var name = $(this).attr('data-toggle-name');
        var hidden = $('input[name="' + name + '"]', form);
        var type = $(this).attr('data-toggle');
        var check = [];
        $('button', $(this)).each(function () {
            $(this).live('click', function () {
                if($(this).val() == hidden.val()) {
                    switch(type) {
                    case 'button':
                        $(this).removeClass('active');
                        hidden.val(0);
                        break;
                    case 'buttons-checkbox':
                        $(this).removeClass('active');
                        break;
                    default:
                        hidden.val($(this).val());
                    }
                } else {
                    switch(type) {
                    case 'button':
                        $(this).addClass('active');
                        hidden.val(1);
                        break;
                    case 'buttons-checkbox':
                        $(this).addClass('active');
                        break;
                    default:
                        hidden.val($(this).val());
                    }
                }
            });
            if($(this).val() == hidden.val()) $(this).addClass('active');
        });
    });
});​

working prototype

  • 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-09T23:00:49+00:00Added an answer on June 9, 2026 at 11:00 pm

    This is how i rewrote your code to make it work well with the check box group:

    jQuery(function($) {
         $('div.btn-group[data-toggle-name]').each(function(){
                var group   = $(this);
                var form    = group.parents('form').eq(0);
                var name    = $(this).attr('data-toggle-name');
                var hidden  = $('input[name="' + name + '"]', form);
                var type    = group.attr('data-toggle');
                var check   = (hidden.val() != '') ? hidden.val().split(',') : [];
                $('button', group).each(function() {
                    var button = $(this);
                    button.live('click', function() {
                        switch(type) {
                            case 'buttons-checkbox':
                                if(check.indexOf(button.val()) == -1){
                                    button.addClass('active');
                                    check.push(button.val());
                                }else{
                                    check.splice(check.indexOf(button.val()),1);
                                    button.removeClass('active');
                                }
                                hidden.val(check.toString());
                                break;
                            case 'button':
                                if(button.val() == hidden.val()) {
                                    button.removeClass('active');
                                    hidden.val(0);
                                } else {
                                    button.addClass('active');
                                    hidden.val(1);
                                }
                                break;
                            default:
                                hidden.val(button.val());
                                break;
                        }
                    });
                    switch(type) {
                        case 'buttons-checkbox':
                            for (i=0;i<check.length;i++) {
                                if(button.val() == check[i]) {
                                    button.addClass('active');
                                }
                            }
                            break;
                        default:
                            if(button.val() == hidden.val()) {
                                button.addClass('active');
                            }
                            break;
                    }
    
                });
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Similar to this question: Checkboxes on Rails What's the correct way of making radio
I try to update the data making use of checkboxes. But when one or
I have 4 checkboxes and I wish to toggle them (checked or unchecked) and
I was making a little test html/php form. Everything is fine here... but the
Making a new site but something is happening to it in IE8. The social
Making a new site but something is happening to it in IE. I've purchased
I'm making a DataGridView with a series of Checkboxes in it with the same
I'm making a restaurant search in which users can filter the search based on
I am making a Java application where I show a couple of checkboxes with
I'm making a game link site, where users can post links to their favorite

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.