Checked checkbox-parent 1.0

Created Thursday 13th of November 2008 by Andreas Lagerkvist
Copyright © 2008 Andreas Lagerkvist (andreaslagerkvist.com)

Please have a look at the "Other Resources" for bug reports or further help on jQuery. Do not post bug reports or feature requests as comments to unrelated articles. If bug reporting on bugtracker.a-framework.org fails, e-mail me.

What it Does

This tiny plug-in adds and removes a 'checked'-class to input[type=checkbox]'s parents. Very useful if you want to style the entire wrapping <label>-element when its child-checkbox is checked.

How to Use

jQuery('form').checkedCheckboxParent(); would apply the plug-in to every checkbox in 'form'.

Example

Example Code

HTML

<div id="jquery-checked-checkbox-parent-example">
<
p>
    <
label>
        <
input type="checkbox" name="dummy" /> Click me to try it
    
</labe>
</
p>
</
div>

JS

jQuery('#jquery-checked-checkbox-parent-example').checkedCheckboxParent();

Source Code

jQuery.fn.checkedCheckboxParent = function () {
    return 
this.each(function () {
        
jQuery(':checkbox'this).each(function () {
            var 
check = $(this);

            var 
checkParent = function () {
                if (
check.is(':checked')) {
                    
check.parent().addClass('checked');
                }
                else {
                    
check.parent().removeClass('checked');
                }
            };

            
checkParent();
            
check.click(checkParent);
        });
    });
};

Download

Plug-in

Requires

Random jQuery Plug-ins

  • Togglable DL

    Use this plug-in on your definition-lists (dl-elements) to allow the dd:s for a dt to be toggled when clicking the dt. Don't use it unless it's semant...

    Details

  • Super Simple Tabs

    This is an extremely basic tabs-plugin which allows you to create tabbed content from the ever-so-common list of in-page-links. The plug-in takes one ...

    Details

  • Live Search

    Use this plug-in to turn a normal form-input in to a live ajax search widget. The plug-in displays any HTML you like in the results and the search-res...

    Details

More Plug-ins

Recent Comments

  1. Coneelolo on "Phu Quoc, Sihanouk Ville, Koh Chang, Koh Wai & Koh Mak"

    For the help please use http://www.google.com

  2. AL on "Accessible, Stylish Modal Windows With Modern CSS"

    @Matt - What exactly isn't working? "Not working" ...

  3. roboteich on "Accessible, Stylish Modal Windows With Modern CSS"

    This is hardly accessible. Screen reader focus is...

Page cached. Loaded in: 0.0143 second(s).
Last DB change: 2010-09-08 19:34:43
Last file change: 2010-08-12 15:31:16
Cache created: 2010-09-09 09:58:16