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;
}

Anu
  • 3
  • 1
  • 4

3 Answers3

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" />

sreejithsdev
  • 1,202
  • 12
  • 26
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.

Community
  • 1
  • 1
Chase
  • 29,019
  • 1
  • 49
  • 48
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

Sushanth --
  • 55,259
  • 9
  • 66
  • 105