0

need to enable a button if the checkbox is enabled using javascript in aspx page. Here is my code in aspx page. Please Help !

<script type="text/javascript">
function checkButt(obj) {
    alert("Inside the function");
    document.getElementById('<%=btnLoadForm.ClientID%>').disabled = !obj.checked;
}

1
  • Does the checkbox has autopostback set to true Commented Nov 30, 2012 at 21:11

3 Answers 3

2

Use following code you will get the solution.

Javascript

<script type="text/javascript">

    function enableordisable() {
            if (document.getElementById('CheckBox1').checked == true) {
                document.getElementById('Button1').disabled = false;
            }
            else {
                document.getElementById('Button1').disabled = true;
            }
    }

HTML

    <asp:CheckBox ID="CheckBox1" runat="server" Text="enable" onchange="enableordisable();" />
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" Text="Button" Enabled="false" />

Sign up to request clarification or add additional context in comments.

Comments

1

Instead of:

document.getElementById('<%=btnLoadForm.ClientID%>').disabled = !obj.checked;

You could try removing the disabled property from the element (add your logic as needed):

document.getElementById('<%=btnLoadForm.ClientID%>').removeAttribute("disabled")

Where disabled is a Boolean attribute, the value doesn't make much of a difference. You can read more on this in a similar post: HTML - Why boolean attributes do not have boolean value?

UPDATE:

You would need logic around the removeAttribute statement. Something similar to:

function checkButt(obj) {
    if(obj.checked)
      document.getElementById('<%=btnLoadForm.ClientID%>').removeAttribute("disabled");
}

This would enable document.getElementById('<%=btnLoadForm.ClientID%>') if obj.checked is checked.

1 Comment

@user1718398 - See if the update above makes things a bit more clear.
0

Remove the AutoPostBack="true" for the checkbox

Add the event in the script instead of inline HTML..

<asp:Button runat="server" id="btnLoadForm" />
​<asp:CheckBox runat="server" id="chk" />​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

JS

var elem = document.getElementById('chk');

chk.addEventListener('click', function(){
    document.getElementById('btn').disabled = !this.checked;
});

Check Fiddle

1 Comment

This works <asp:CheckBox ID="chkEnableButton" runat="server" OnClick="checkButt(this)"; Text="I agree" />

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.