Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap radio button "checked" flag

In case #1 works, in case #2 it do not works. Check the code bellow:

<div class="container">     <div class="row">         <h1>Radio Group #1</h1>         <label class="radio-inline">           <input name="radioGroup" id="radio1" value="option1" type="radio"> 1         </label>         <label class="radio-inline">           <input name="radioGroup" id="radio2" value="option2" checked="checked" type="radio"> 2         </label>         <label class="radio-inline">           <input name="radioGroup" id="radio3" value="option3" type="radio"> 3         </label>     </div>     <div class="row">         <h1>Radio Group #2</h1>         <label for="year" class="control-label input-group">Year</label>         <div class="btn-group" data-toggle="buttons">             <label class="btn btn-default">                 <input name="year" value="2011" type="radio">2011             </label>             <label class="btn btn-default">                 <input name="year" value="2012" type="radio">2012             </label>             <label class="btn btn-default">                 <input name="year" value="2013" checked="checked" type="radio">2013             </label>         </div>       </div>   </div> 

You can see it in action here: http://bootply.com/84165

like image 235
Khrys Avatar asked Sep 30 '13 20:09

Khrys


People also ask

How do I get checkbox checked in bootstrap?

Example Explained form-check-input to style checkboxes properly inside the . form-check container. Use the checked attribute if you want the checkbox to be checked by default.

How do you put a space between radio button and text in bootstrap?

Edit: You can just separate the input and label and link them using an 'id' on the input and a 'for' attribute on the label. Then you can style your label to add the spacing. Note: the <input> tag does not use and does not need a closing slash and never has in HTML.


2 Answers

Assuming you want a default button checked.

<div class="row">     <h1>Radio Group #2</h1>     <label for="year" class="control-label input-group">Year</label>     <div class="btn-group" data-toggle="buttons">         <label class="btn btn-default">             <input type="radio" name="year" value="2011">2011         </label>         <label class="btn btn-default">             <input type="radio" name="year" value="2012">2012         </label>         <label class="btn btn-default active">             <input type="radio" name="year" value="2013" checked="">2013         </label>     </div>   </div> 

Add the active class to the button (label tag) you want defaulted and checked="" to its input tag so it gets submitted in the form by default.

like image 155
Trevor Avatar answered Sep 23 '22 10:09

Trevor


A javascript fix to apply the 'active' class to all labels that are parents of checked inputs:

$(':input:checked').parent('.btn').addClass('active'); 

insert right after

$('.btn').button(); 
like image 30
rawrkats Avatar answered Sep 23 '22 10:09

rawrkats