1

HTML Code:

<div class="checkbox" style=" float:left "></div>
<input type="checkbox" class="realcheckbox" id="financing"  />

jQuery Code:

$(".checkbox").click(function() { 
    var xchecked = $(this).siblings(".realcheckbox").attr("checked");
    if (xchecked == false) {
        $(this).css("background-image","url('checkboxselected.png')");
        $(this).next(".realcheckbox").attr("checked",true);
    }
    else {
        $(this).css("background-image","url('checkbox.png')");
        $(this).next(".realcheckbox").attr("checked",false);
    }
});

Clicking on the fake checkbox isn't checking the real one, but if the real one is checked, clicking on the fake one is doing the job, means the real one gets unchecked, what's wrong with the first one ?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Ali Bassam
  • 9,691
  • 23
  • 67
  • 117
  • 4
    This is answered here http://stackoverflow.com/questions/901712/check-checkbox-checked-property-using-jquery – Oliver M Grech May 10 '12 at 11:00
  • Remember that you can check checkboxes in other ways than clicking on it. – David Hellsing May 10 '12 at 11:05
  • This is answered in the below page mate, [http://stackoverflow.com/questions/901712/check-checkbox-checked-property-using-jquery](http://stackoverflow.com/questions/901712/check-checkbox-checked-property-using-jquery) Have a nice day! – Oliver M Grech May 10 '12 at 11:02

5 Answers5

9

demo http://jsfiddle.net/ktwDK/3/

good read: http://api.jquery.com/checked-selector/

.is(":checked") will do the trick for you.

jquery code

$(".realcheckbox").click(function(){
    alert($(this).is(":checked"));
});​
Tats_innit
  • 33,991
  • 10
  • 71
  • 77
3

Use is(":checked") to test:

var xchecked = $(this).siblings(".realcheckbox").is(":checked");
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

Maybe you need to check pseudo for that:

var xchecked = $(this).siblings(".realcheckbox").is(":checked");
antyrat
  • 27,479
  • 9
  • 75
  • 76
0
$('.checkBox').click(function() {
   var thisCheck = $(this);
 if (thisCheck.is(':checked')) {
//do your stuff
  }
});
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
0
$(".checkbox").click(function() { 
    var xchecked = $(this).find(".realcheckbox").prop("checked");
    if (xchecked == false) {
        $(this).css("background-image","url('checkboxselected.png')");
        $(this).next(".realcheckbox").attr("checked",true);
    }
    else {
        $(this).css("background-image","url('checkbox.png')");
        $(this).next(".realcheckbox").attr("checked",false);
    }
});
Thulasiram
  • 8,432
  • 8
  • 46
  • 54