24

How to enable button when checkbox clicked in jQuery?

1
  • 1
    Show some effort here and post your code. Otherwise close. Commented Aug 25, 2010 at 12:15

3 Answers 3

62

You can do it like this:

$("#checkBoxID").click(function() {
  $("#buttonID").attr("disabled", !this.checked);
});

This enables when checked, and disables again if you uncheck. In jQuery .attr("disabled", bool) takes a boolean, so you can keep this pretty short using the this.checked DOM property of the checkbox.

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

2 Comments

+1 for the negating the boolean, it's a simple technique that isn't used enough, IMO. Oh, and not using .is(":checked"), too ;-)
As of jQuery 1.6 it's recommended to use the .prop() method for this instead (api.jquery.com/attr).
5
$("#yourcheckboxid").click(function() {
    var checked_status = this.checked;
    if (checked_status == true) {
       $("#yourbuttonid").removeAttr("disabled");
    } else {
       $("#yourbuttonid").attr("disabled", "disabled");
    }
});

Comments

0

If you are also using Bootstrap with Jquery, "disabled" is not an attribute but a class. So, (and I usually keep my functions on a separate loadable file to keep it clean) do this:

  1. Create whatever.js

  2. Put this code in it:

    function seethemagic(){ $('#btn-send').toggleClass('disabled'); }

  3. Load whatever.js into your html

  4. The button can look like this:

    <button id="btn-send" class="btn btn-primary d-grid w-100 disabled" onclick="seethemagic();">Show me the magic</button>

This way the button will toggle on and off.

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.