Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable/Disable submit button if checkbox is checked/unchecked?

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>
like image 283
HelpNeeder Avatar asked Apr 05 '12 02:04

HelpNeeder


People also ask

How do I disable the submit button when the field is empty?

Just click f12 in your browser, find the submit button in the html, and then remove the disabled ! It will submit the form even if the inputs are empty.

How do I make my submit button disabled?

1.1 To disable a submit button, you just need to add a disabled attribute to the submit button. $("#btnSubmit"). attr("disabled", true); 1.2 To enable a disabled button, set the disabled attribute to false, or remove the disabled attribute.


2 Answers

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.

like image 173
Paul Bruno Avatar answered Sep 27 '22 21:09

Paul Bruno


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

 <input type="checkbox" name="TOS" value="Accept" onClick="EnableSubmit()"> I agree to Terms of Service agreement.
like image 41
RyanS Avatar answered Sep 27 '22 22:09

RyanS