0

I am new to html and Jquery I am trying to create a project where in first I disable my Jquery Button first but when checkbox clicked it will be enabled however my code below is not working. The button is not disable always enable even my checkbox is uncheck. I would really appreciate any suggestion and help. Thanks a lot.

function goFurther() {
  if (document.getElementById("chkAgree").checked == true) {
    $("#submit").disabled = false;
    $("#cancel").disabled = false;  
  } else {
    $("#submit").disabled = true;
    $("#cancel").disabled = true;
  }
}
input[disabled] {
  color: gray;
  text-decoration: none;
}
<form>
  <input type="checkbox" id="chkAgree" onclick="goFurther()">I agree.
  <br><br>
</form>

1

2 Answers 2

2

You cannot set disabled on a jQuery wrapped element. Use prop() for it:

$("#submit").prop('disabled', false); // To enable the button
$("#submit").prop('disabled', true); // To disable the button

Alternatively, you can get the native DOM elements out of the jQuery wrapper using get() and then assign disabled:

$("#submit").get(0).disabled = false; // To enable the button
$("#submit").get(0).disabled = true; // To disable the button
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much it still not working when I placed it in my code I really appreciate your help
@CLOUDS -- Please specify what's not working. It is too difficult to track down a bug without knowing much of your code. Did you see any error in console?
0

There are many ways to disable checkbox with and without jquery like

document.getElementById("test").disabled= true;
$("#test").prop('disabled', true);
$("#test").attr('disabled', true);

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.