How to enable button when checkbox clicked in jQuery?
3 Answers
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.
2 Comments
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.