Checked checkbox-parent 1.0

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

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

blog comments powered by Disqus

Random jQuery Plug-ins

  • Favicons

    This little plug-in will insert favicons to all external links found on your site. The plug-in scans the URL the link is pointing to for a favicon and...

    Details

  • Image Zoom

    This plug-in makes links pointing to images open in the "Image Zoom". Clicking a link will zoom out the clicked image to its target-image. Click anywh...

    Details

  • Captcha Refresh

    If you use a so called CAPTCHA-image on your site then you can use this plug-in to allow users to click your CAPTCHA in order to generate a new random...

    Details

More Plug-ins

Recent Comments

Powered by Disqus
Page cached. Loaded in: 0.0261 second(s).
Last DB change: 2012-04-02 11:06:05
Last file change: 2012-04-25 20:30:39
Cache created: 2012-05-18 06:15:07