Javascript :checked for Bootstrap

Bring Bootstrap's components to life—now with 6 custom CSS plugins that (ab)use checkboxes.

Questionable use of :checked pseudo-class

I created bootleg-*.css as demonstration of :checked for a talk, seriously don't use it. Except if you're doing custom checkbox/radios. See GitHub repo.

Examples

Modals are streamlined, but flexible, dialog prompts with the minimum required functionality and smart defaults.

Static example

A rendered modal with header, body, and set of actions in the footer.

<div class="modal hide fade">
  <div class="modal-header">
    <label role="button" class="close" for="closemodal">×</label>
    <h3>Modal header</h3>
  </div>
  <div class="modal-body">
    <p>One fine body…</p>
  </div>
  <div class="modal-footer">
    <label role="button" class="close" for="closemodal">Close</label>
    <a href="#" class="btn btn-primary">Save changes</a>
  </div>
</div>

Live demo

Toggle a modal via :checked by clicking the button below. It will slide down and fade in from the top of the page.

<-- Button to trigger modal -->
<label for="modal1" role="button" class="btn">Launch demo modal</label>
<input type="radio" id="modal1" name="modal" />

<-- Modal -->
<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <label role="button" class="close" for="closemodal">×</label>
    <h3 id="myModalLabel">Modal header</h3>
  </div>
  <div class="modal-body">
    <p>One fine body…</p>
  </div>
  <div class="modal-footer">
    <label role="button" class="close" for="closemodal">Close</label>
    <button class="btn btn-primary">Save changes</button>
  </div>
</div>

Usage

Via for attribute

Activate a modal without writing JavaScript. Set for="modal1" on a controller label preceded by a radio input.

<label for="modal1" role="button" class="btn">Launch modal</label>

Via JavaScript

Call a modal with id modal1 with a single line of JavaScript:

$('#modal1').prop("checked")

Example tabs

Add quick, dynamic tab functionality to transiton through panes of local content, even via dropdown menus.

Raw denim you probably haven't heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache cliche tempor, williamsburg carles vegan helvetica. Reprehenderit butcher retro keffiyeh dreamcatcher synth. Cosby sweater eu banh mi, qui irure terry richardson ex squid. Aliquip placeat salvia cillum iphone. Seitan aliquip quis cardigan american apparel, butcher voluptate nisi qui.

Food truck fixie locavore, accusamus mcsweeney's marfa nulla single-origin coffee squid. Exercitation +1 labore velit, blog sartorial PBR leggings next level wes anderson artisan four loko farm-to-table craft beer twee. Qui photo booth letterpress, commodo enim craft beer mlkshk aliquip jean shorts ullamco ad vinyl cillum PBR. Homo nostrud organic, assumenda labore aesthetic magna delectus mollit. Keytar helvetica VHS salvia yr, vero magna velit sapiente labore stumptown. Vegan fanny pack odio cillum wes anderson 8-bit, sustainable jean shorts beard ut DIY ethical culpa terry richardson biodiesel. Art party scenester stumptown, tumblr butcher vero sint qui sapiente accusamus tattooed echo park.


Usage

You can activate individual tabs in several ways:

$('#myTab #profile').prop('checked'); // Select tab by name
$('#myTab input:first-of-type').prop('checked'); // Select first tab
$('#myTab input:last-of-type').prop('checked'); // Select last tab

Markup

Each "tab" contains an input and a label, they cannot be nested as it'll defeat the trick of using :checked and CSS doesn't have a parent selector yet.

<input type="radio" id="home" name="tabs" checked />
<label for="home">Home</label>

<input type="radio" id="profile" name="tabs" />
<label for="profile">Profile</label>

<input type="radio" id="other" name="tabs" />
<label for="other">Other</label>

<input type="radio" id="misc" name="tabs" />
<label for="misc">Misc</label>

Example uses

Do more with buttons. Control button states or create groups of buttons for more components like toolbars.

Single toggle

Add data-toggle="button" to activate toggling on a single button.

<input type="checkbox" id="toggle" data-toggle="button">
<label for="toggle" class="btn">Single Toggle</label>

Checkbox

Using checkboxes to allow toggling of multiple buttons in a group.

<div class="btn-group" data-toggle="buttons-checkbox">
  <input type="checkbox" id="group1" data-toggle="button">
  <label class="btn btn-primary" for="group1">Left</label>
  
  <input type="checkbox" id="group2" data-toggle="button">
  <label class="btn btn-primary" for="group2">Middle</label>
  
  <input type="checkbox" id="group3" data-toggle="button">
  <label class="btn btn-primary" for="group3">Right</label>
</div>

Radio

Use radios to only allow one button in a group to be toggled at a time.

<div class="btn-group" data-toggle="buttons-radio">
  <input type="radio" id="radio1" name="radio-group" data-toggle="button">
  <label class="btn btn-primary" for="radio1">Left</label>
  
  <input type="radio" id="radio2" name="radio-group" data-toggle="button">
  <label class="btn btn-primary" for="radio2">Middle</label>
  
  <input type="radio" id="radio3" name="radio-group" data-toggle="button">
  <label class="btn btn-primary" for="radio3">Right</label>
</div>

About

Get base styles and flexible support for collapsible components like accordions and navigation.

* Requires the Transitions plugin to be included.

Example accordion

Using the collapse plugin, we built a simple accordion style widget:

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
<input type="radio" id="collapse1" name="accordion-group" checked>
<label for="collapseAll" class="accordion-dismiss"></label>
<div class="accordion-heading">
  <label for="collapse1" class="accordion-toggle">Collapsible Group Item #1</label>
</div>
<div id="collapseOne" class="accordion-body collapse">
  <div class="accordion-inner">
    ...
  </div>
</div>

Usage

Via checked attribute

To make one open by default set the checked attribute on the radio input.