3

I have this check box:

<div class="OuterDiv">
   <div class="InnerDiv">
      <input type="checkbox" name="tie" value="tie">Error in tie score<br>
   </div>
</div>

I have this jquery to toggle the checkbox:

$(document).ready(function() {
   $('.OuterDiv').click(function() {
      if ($(this).find('input:checkbox').is(':checked')) { 
         $(this).find('input:checkbox').attr('checked',false);
      } else {
         $(this).find('input:checkbox').attr('checked',true);
      }
   });
});

This seems to work, to toggling the checkbox when clicking on the div. However when I click the checkbox itself, nothing happens and it does not toggle.

5
  • 1
    Why aren't you just using a <label> around the checkbox? Commented Mar 1, 2013 at 19:58
  • Your problem is that when you click on check box, check box itself toggles, then another toggle is done because of your code! Commented Mar 1, 2013 at 20:04
  • Side note: use .prop() rather than .attr(). Commented Mar 1, 2013 at 20:05
  • You should use label. Its there for a reason. Also, When you click the checkbox, it checks the checkbox (the default functionality), but the event is propagated to Outerdiv and thus your function toggles it and you see no change Commented Mar 1, 2013 at 20:06
  • take a look at <a href="stackoverflow.com/questions/426258/… do I check a checkbox with jQuery or JavaScript?</a> which says to use .prop() to jQuery 1.6+ and use .attr() to jQuery 1.5 and below Commented Mar 1, 2013 at 20:20

3 Answers 3

2

Without a <label> around the input (entire DIV is clickable) go for:

LIVE DEMO

$(document).ready(function() {

   $('.OuterDiv').click(function(e) {
     var $chkb = $(':checkbox', this)[0];
     if(e.target !== $chkb) $chkb.checked = !$chkb.checked; 
   });

});

(No JS:)
Else, if you just want the text next to the checkbox to be clickable:
LIVE DEMO

<div class="OuterDiv">
   <div class="InnerDiv">
     <label>
      <input type="checkbox" name="tie" value="tie">Error in tie score<br>
     </label>
   </div>
</div>
Sign up to request clarification or add additional context in comments.

5 Comments

Not if there's a bunch of other stuff in OuterDiv
Sorry I disagree. Are you sure your solution works w/o looking at op's CSS?
What I mean: jsbin.com/owewey/7/edit -- you're making an assumption that op wants a label.
@Madbreaks, You are making an assumption that op doesnt want a label :) +1 to compensate.
I posted the similar solution in comment to @wirey's answer
1

You need to stop the propagation of the event bubbling up from your checkbox using event.stopPropagation()

$('input[type=checkbox]').click(function(e){
    e.stopPropagation();
});

2 Comments

Right. Cant this can be merged into the existing click by e.target ?
@Jashwant I wouldn't know how that would work.. Can you make a fiddle with that technique?
0

You don't need to use jQuery for this. Just use label tag as below:

<div class="OuterDiv">
    <label>
        <input type="checkbox" name="tie" value="tie">Error in tie score
    </label>
</div>

1 Comment

I guess, you dont see any answer / comment before posting your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.