Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML

nicholas maddren
nicholas maddren
12,793 Points

Make checkbox container clickable

Hey guys, I'm a little confused at the moment.

How can I make the container of the checkbox clickable?

Here is an example of my form: http://jsfiddle.net/7r4pzbzg/1/

As you can see only the checkbox and the label is clickable, how can I make the actual container that it sits in clickable? I have tried making the label width 100% however it sits on a different line when that is done.

Any ideas? Thanks

2 Answers

Jay Mayu
Jay Mayu
6,805 Points

Checkout this solution at http://jsfiddle.net/7r4pzbzg/4/

This is where the decision is made and checkbox checked / unchecked

$('.panel').on('click',function(){
        if(checkBoxChecked){
            checkBoxChecked = false;
            $('#bodytype-checkbox').prop('checked', false);
        }else{
            checkBoxChecked = true;
            $('#bodytype-checkbox').prop('checked', true);
        }
});

I declared a global boolean variable to track whether the checkbox is checked or not. if it's checked then I programmatically uncheck it and viceversa.

nicholas maddren
nicholas maddren
12,793 Points

Hey, thanks I tried it and it works well however the label cannot be clicked to check the box :(

you could set your label to a block element. This pushes the checkbox away, but you can float the checkbox to the left. Try to add this to your css:

label{
    display: block;
    padding-left: 20px;
}
#bodytype-checkbox{
    float:left;
}