0

I created a checkbox:

<input type="checkbox" id="populatie" name="werking">

But when I try to check if it's checked, it doesn't do anything.

$("#opslaan").click(function(){

    if ($("#populatie").checked) {
    alert(hello); } 


})

What am I doing wrong?

Edit: when I inspect element, and I checked it, I get:

checked true

So it's definetly checked...

user3117628
  • 776
  • 1
  • 15
  • 35
  • 1
    Try: `$("#populatie")[0].checked` [DEMO](http://jsfiddle.net/tewathia/Kp7Wu/). The `checked` property is for DOM nodes, not jQuery wrapped sets – tewathia Feb 01 '14 at 12:38
  • duplicate: http://stackoverflow.com/questions/901712/check-checkbox-checked-property-using-jquery – Viktor K Feb 01 '14 at 12:39

5 Answers5

1

Check it without jQuery:

$("#opslaan").click(function(){
    if(document.getElementById('populatie').checked) {
        alert("hello");
    }
});

It may even be faster

SomeShinyObject
  • 7,581
  • 6
  • 39
  • 59
0

$("#populatie") returns a jQuery object, which does not have the checked property, it belongs to the dom element.

There are many ways to check it, one of the is to get the dom element reference and then check whether the property is set

Another is to use .is() and the :checked selector

$("#opslaan").click(function(){

    if ($("#populatie").is(':checked')) {
    alert(hello); } 


})
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

you must pass hello as string to alert: alert("hello"), double quotation mark is missing

Amir Sherafatian
  • 2,083
  • 2
  • 20
  • 32
0

Use prop() , $("#populatie") is a jQuery object,which doesn't have checked property

$("#opslaan").click(function(){    
    if ($("#populatie").prop('checked')) {
    alert('hello'); 
   }     
});

DEMO

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
0
if ($("#populatie").is(":checked"){

    alert("");

}
CRABOLO
  • 8,605
  • 39
  • 41
  • 68
Manoj
  • 56
  • 3