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

  • Drag to Select

    Use this plug-in to allow your users to select certain elements by dragging a "select box". Works very similar to how you can drag-n-select files and ...

    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

  • Slide Presentation

    A sort of slide/presentation kind of plug-in. Displays sliding images with text floating on top. This one's pretty specific but perhaps someone will f...

    Details

More Plug-ins

Recent Comments

  1. Andreas on "SleekPHP User Handling"

    Thank you for showing interest :) Were the docs en...

  2. ciptard on "SleekPHP User Handling"

    Ok, thank you

  3. Andreas on "SleekPHP User Handling"

    I might write a "getting started" article for Slee...

Page cached. Loaded in: 0.0331 second(s).
Last DB change: 2012-02-04 08:04:40
Last file change: 2011-10-03 07:42:35
Cache created: 2012-02-04 09:20:51