Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery select checkbox by div

Tags:

jquery

I'm wondering if there is a way in jQuery to check/uncheck a checkbox when someone clicks an entire div layer. Like having a massive selection area, essentially.

Any ideas?

Here is an example... I am trying to make the around the checkbox clickable to toggle the individual checkbox, pretty much.

<fieldset>
    <div>
        <input type="checkbox" id="Checkbox1" />
    </div>
    Person 1<br />
</fieldset>
<fieldset>
    <div >
        <input type="checkbox" id="Checkbox2" />
    </div>
    Person 2<br />
</fieldset>
like image 653
Ciel Avatar asked Dec 05 '22 04:12

Ciel


1 Answers

Perhaps use the clicked div as the parent.

$(function() {
     $('#divId').toggle(
       function(event) {
         $(this).find('input').attr('checked', true);
       },
       function(event) {
         $(this).find('input').attr('checked', false);
       }
     );
 });

This should only check the boxes inside the #divId that's clicked.

like image 108
Joel L Avatar answered Jan 08 '23 14:01

Joel L