14

How to enable the submit button in my HTML form and disable if checkbox was unchecked?

What is wrong with this code?

EnableSubmit = function(val)
{
    var sbmt = document.getElementById("Accept");

    if (val.checked == true)
    {
        sbmt.disabled = false;
    }
    else
    {
        sbmt.disabled = true;
    }
}                       

The check box

<td width="30%">Do you accept Terms of Service agreement?</td>
<td width="10px" style="min-width: 10px"></td>
<td width="70%">
    <input type="checkbox" name="TOS" value="Accept" onClick="EnableSubmit"> I agree to Terms of Service agreement.
</td>
HelpNeeder
  • 6,383
  • 24
  • 91
  • 155
  • possible duplicate of [How do i disable a submit button when checkbox is uncheck?](http://stackoverflow.com/questions/5458531/how-do-i-disable-a-submit-button-when-checkbox-is-uncheck) – Ciro Santilli OurBigBook.com Jul 06 '14 at 10:59

3 Answers3

16

Change your HTML to this:

<td width="30%">Do you accept Terms of Service agreement?</td>
<td width="10px" style="min-width: 10px"></td>
<td width="70%">
    <input type="checkbox" name="TOS" value="Accept" onClick="EnableSubmit(this)"> I agree to Terms of Service agreement.
</td>

Reason it's not working: you're not passing your function anything. Actually, because you've not used parentheses with the function name, you've not called the function at all. Passing it this in the context of the onclick HTML event attribute means you're passing a reference to the element's DOM object, giving you access to its checked property.

Paul Bruno
  • 1,896
  • 1
  • 17
  • 26
  • 2
    Could you re-enter your question to include the rest of the source for the form (the HTML)? And any other JavaScript you might be using? – Paul Bruno Apr 05 '12 at 05:41
3

You need to include the parameters: onClick="EnableSubmit()"

 <input type="checkbox" name="TOS" value="Accept" onClick="EnableSubmit()"> I agree to Terms of Service agreement.
RyanS
  • 3,964
  • 3
  • 23
  • 37
0

First if condition checks whether toggle is switched to desired side. Condition can be changed according to requirement. Last jquery is to make toggle button grey.

 if(!$('#togglehk').is(":checked"))
                    {
                        $('#togglehk').click();
                    }
  jQuery('#togglehk').attr("disabled", true);
  jQuery('#togglehk').prop("disabled",true);

  jQuery('#togglehk').next(".slider").css("backgroundcolor","#B3B3B3");
shubhranshu
  • 399
  • 3
  • 7