Reputation: 6085
How to enable button when checkbox clicked in jQuery?
Upvotes: 24
Views: 36013
Reputation: 281
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:
Create whatever.js
Put this code in it:
function seethemagic(){ $('#btn-send').toggleClass('disabled'); }
Load whatever.js into your html
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.
Upvotes: 0
Reputation: 630607
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.
Upvotes: 62
Reputation: 9489
$("#yourcheckboxid").click(function() {
var checked_status = this.checked;
if (checked_status == true) {
$("#yourbuttonid").removeAttr("disabled");
} else {
$("#yourbuttonid").attr("disabled", "disabled");
}
});
Upvotes: 5