1

How can I check if checkbox is not checked with jQuery? Is there something that is negative of .click(....)?

Here is my code:

$().ready(function () {
        // validate UserSettings form on keyup and submit
        $('#CheckBox').click(function () {
            $("#TextBox").validate({
                rules: {
                    NextLocation: {
                        required: true
                    }
                }
            });
        });
    });

This code works in reverse than what I want (if sets true when checkbox is checked). I want required to not be set to true if checkbox is checked

2
  • Did you try just typing false instead of true ? Commented Nov 19, 2013 at 18:36
  • It would be best to also show your HTML markup. Commented Nov 19, 2013 at 21:36

5 Answers 5

1

You can use ! to make sure that this gets executed when the current condition is false. So, when you write a situtaion where the code get executed when the checkbox is checked, just add ! in the start of the code as I did. And you'll get the code working. Other functions will be just the way they are .click() and so on.

$('input[type=checkbox]').click(function () { // use #CheckBox if you want to
   if(!$(this).prop('checked')) { // if NO checkbox checked
     $('div').html("checked");
   }
})

This will look if the checkbox isn't checked!

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

Comments

1

Your code...

$().ready(function () {
    // validate UserSettings form on keyup and submit
    $('#CheckBox').click(function () {
        $("#TextBox").validate({
            rules: {
                NextLocation: {
                    required: true
                }
            }
        });
    });
});

... has the following issues...

1) $().ready() is not recommended as per jQuery docs. Use $(document).ready() instead.

2) You cannot attach .validate() to an input element. It only gets attached to the <form> element.

3) The .validate() method is only used for initializing the plugin, not for testing anything. Therefore, it typically does not belong inside of a click handler. You would fire .validate() only once as soon as the form is constructed, typically within the DOM ready handler.

4) If you want to make something required depending on the state of something else, you would use the depends method within the required rule.

My working demo below shows that the text field named NextLocation is only required when the checkbox named check is not checked:

$(document).ready(function () {  // DOM ready handler

    $('#myform').validate({  // attach to form to initialize plugin with options
        rules: {
            NextLocation: {
                required: {
                    depends: function () {
                        return !$('input[name="check"]').is(':checked');
                    }
                }
            }
        }
    });

});

HTML:

<form id="myform">
    Checkbox: <input type="checkbox" name="check" />
    NextLocation: <input type="text" name="NextLocation" />
</form>

DEMO: http://jsfiddle.net/dymmw/

Comments

0

Try this

if($("#CheckBox").prop('checked')){
    //do something
}

Comments

0

Try this inside your if statement

 $('#checkbox').is('checked')

Comments

0

You have two approaches:

A - You can change it using the click function an checking there:

...
('#CheckBox').click(function () {
   if($(this).is(':checked')){
       ...
   }
});
...

B - Use the change function, which I'd prefer

$("#CheckBox").change(function(){
   if($(this).checked){
      ...
   }

});

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.