1

I am using the following html to display a checkbox with an image:

<div class="testClass">
<input id="111" name="compare_checkbox" type="checkbox" />                      
<label for="111">
    <i class="icon" aria-hidden="true"></i> <b>Compare</b>
</label>
</div>

I need to uncheck the checkbox in certain conditions. I am using the following jquery : $('#111')[0].checked = false;

This works fine if I don't have the label. When I have the label, I don't get any errors in console (either Chrome or Firefox), but the checkbox does not uncheck.

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
klone
  • 2,005
  • 2
  • 16
  • 18

4 Answers4

1

Just letting you know that you cannot have numbers leading your element ID. Please see Element ID in HTML Document ~ Naming Question. Hope this is helpful to you :)

Solution:

<div class="testClass">
<input id="one" name="compare_checkbox" type="checkbox" />                      
<label for="one">
    <i class="icon" aria-hidden="true"></i> <b>Compare</b>
</label>
</div>

http://jsfiddle.net/tLsK7/

Community
  • 1
  • 1
3066d0
  • 1,853
  • 1
  • 14
  • 18
0

Checkbox states are best handled cross-browser with jQuery's prop function, regardless of whether or not the checkbox has an associated <label>.

$('#element').prop("checked", true); // Checks a checkbox.
$('#element').prop("checked", false); // Unchecks a checkbox.
theftprevention
  • 5,083
  • 3
  • 18
  • 31
  • There must be something else in your JavaScript preventing it from working, because it works with the label present [**in this fiddle.**](http://jsfiddle.net/vjHXT/1/) – theftprevention Sep 17 '13 at 22:16
  • 2
    Also, keep in mind that, according to everything below HTML5, `'111'` **is not a valid ID** for an element. Unless you're using HTML5, the ID must start with a letter. – theftprevention Sep 17 '13 at 22:17
0

working fine with for='111'

Here it is http://jsfiddle.net/spaipalle/umAsw/3/

$('#111').click(function(){
if($('#111').is(":checked")){
        alert('checked');
        $('#111')[0].checked = false; // uncheck it
}else{
        alert('unchecked');
}
});
Swathi
  • 323
  • 1
  • 4
  • 11
0

have you tried changing the id of the element? I don't think ID's can be made up of just numbers, or at least they can't start with a number

Terry Kernan
  • 746
  • 5
  • 12