Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap checkbox/radio buttons do not change <input> value

Tags:

The JavaScript page for Bootstrap shows some nice use of buttons to style checkboxes and radio fields. For example, for a checkbox, I might write

<div class="btn-group" data-toggle="buttons">   <label class="btn btn-primary">     <input type="checkbox"> Option 1   </label> </div> 

However, the library doesn't actually change the value of the underlying <input> field -- it just changes whether the <label> field has class active. I would have expected it to change the checked attribute on the checkbox. Apparently I don't just have it misconfigured -- this is the way the examples on the Bootstrap site work.

Is this actually expected behavior? If so, it seems fairly useless, as people are going to want to use the checkbox field. If not, how do I properly configure Bootstrap checkboxes/radio buttons?

like image 276
jameshfisher Avatar asked Aug 22 '13 23:08

jameshfisher


People also ask

What is the difference between the radio button and checkbox controls?

Checkboxes and radio buttons are elements for making selections. Checkboxes allow the user to choose items from a fixed number of alternatives, while radio buttons allow the user to choose exactly one item from a list of several predefined alternatives.

How do I keep my radio button checked?

You can check a radio button by default by adding the checked HTML attribute to the <input> element. You can disable a radio button by adding the disabled HTML attribute to both the <label> and the <input> .

Which method returns the state of the checkbox or radio button?

Hey Priya, to check the state of a radio button or checkbox, you can use isSelected() method of Selenium Webdriver, which returns the boolean value as output.


2 Answers

The checked attribute on the input isn't modified because that isn't what changes when a checkbox input is checked -- the checked DOM property is what changes (true or false), and Bootstrap handles this properly (you can inspect the element in Firebug and see the DOM property change when you toggle them). The checked attribute is only used to determine default value when the DOM is initially rendered.

If you ever happen to be doing any js/jQuery with checkboxes/radios, remember this! If you need to programatically check a checkbox or radio button, $('input').attr('checked', 'checked'); will not get the job done. $('input').prop('checked', true); is what you need.

This isn't special behavior for Bootstrap buttons, this is how all checkbox/radios work.

Edit: Firebug screenshot

Edit 2: Added text from the comments, as it seems to be helpful.

like image 184
tomaroo Avatar answered Oct 30 '22 07:10

tomaroo


It looks like you are missing a call to "activate" the btn-group (documentation). Here is a demo of this working:

<form id='testForm'>     <div class="btn-group" data-toggle="buttons">         <label class="btn btn-primary">             <input type="checkbox" name='Option' value='1' />Option 1</label>     </div> </form> <button class='btn' id='actionSubmit'>Submit</button>  <script>     $('#actionSubmit').on('click', function (e) {         e.preventDefault();         alert($('#testForm').serialize());     });      $('.btn-group').button(); </script> 
like image 44
Jason Sperske Avatar answered Oct 30 '22 07:10

Jason Sperske