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...

2
  • 1
    Try: $("#populatie")[0].checked DEMO. The checked property is for DOM nodes, not jQuery wrapped sets Commented Feb 1, 2014 at 12:38
  • duplicate: stackoverflow.com/questions/901712/… Commented Feb 1, 2014 at 12:39

5 Answers 5

1

Check it without jQuery:

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

It may even be faster

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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); } 


})

Comments

0

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

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

DEMO

Comments

0
if ($("#populatie").is(":checked"){

    alert("");

}

Comments

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.