0

I currently have this layout:

<div class="row">
    <div class="col-lg-12">

        <div class="input-group">
            <div class="input-group-addon">
                <input class="checkboxOption" type="checkbox">
            </div>
            <div class="form-control">Clinical Pharmacology (sub req'd)</div>
        </div><!--input-group-->

        <div class="input-group">
            <div class="input-group-addon">
                <input class="checkboxOption" type="checkbox">
            </div>
            <div class="form-control">Clinicaltrials.gov</div>
        </div><!--input-group-->
    </div>
</div>

and I want to be able to just click the input-group or the form-control and check the checkbox nested inside.

I currently have this in my popup.js:

$(".form-control").on("touchstart", checkInside);

function checkInside(e) {
    var box = $(".input-group > .input-group-addon > .checkboxOption");
    if(box.checked){
        box.attr('checked', false);
     } else{
         box.attr('checked', true);
     }
 }

but when I place console.log statements in the function, it seems as if the click isn't even registering. is there another approach to this, or something else?

BBH1023
  • 515
  • 2
  • 7
  • 18

3 Answers3

1

I've made a Jsfiddle here

use :

prop('checked', false);

instead of :

box.attr('checked', false);

touchstart will work only on mobile (or debug mode).

Thom-x
  • 841
  • 1
  • 6
  • 20
  • thank you so much. this is the answer i am looking for. there are a LOT of checkboxes, changing the html would not be fun – BBH1023 Sep 08 '14 at 16:40
0

What you want can be achieved with <label> elements tied to the ids of the checkboxes. Basically wrap a <label> around everything you want to be able to click to check a checkbox, and add for="checkboxID" where checckboxID is the id of the checkbox.

Here's a fiddle.

flowstoneknight
  • 1,248
  • 1
  • 7
  • 6
0

You don't really need jQuery or Javascript to make it work.

Example:

<input id="checkboxID" type="checkbox" />
<label for="checkboxID">This will check/uncheck the checkbox with the ID 'checkboxID' if clicked</label>

or

<input id="checkboxID2" type="checkbox" />
<div>
   <label for="checkboxID2">This will check/uncheck the checkbox with the ID 'checkboxID2' if clicked</label>
</div>

jsFiddle

koralarts
  • 2,062
  • 4
  • 30
  • 48