views:

61

answers:

3

I tried this javascript but it doesn't work - here

I need to change the button's enabled property to true when the checkbox is checked and to false when it isn't. This is my code.

<tr>
  <td colspan="2" align="center">
    <asp:CheckBox ID="cbAcceptAgreement" runat="server" OnClientClick="acceptAgreement(this)" />
    <asp:Label ID="lblUserAgreement" runat="server" Text="I accept the " />
    <asp:HyperLink ID="hlUserAgreement" runat="server" Text="User Agreement" NavigateUrl="Help/UserAgreement.aspx" />
  </td>
</tr>
<tr>
  <td colspan="2" align="center">
    <asp:Button ID="btnRegister" runat="server" Text="Register"  />
  </td>
</tr>

<script type="text/javascript">
  function acceptAgreement(obj) {
    document.getElementById('<%=btnRegister.ClientID%>').disabled = !obj.checked;
  }
</script>

Can you help me solve this problem?

A: 
function acceptAgreement(id){ // where id is check box id
  var check = document.getElementById(id).checked
  document.getElementById('btnRegister').disabled = !check
}
Salil
where `"master"` is `id`, hey?
David Hedlund
@david. It's by mistake i edit it.
Salil
+2  A: 

Javascript is case sensitive, you have Obj and obj in your function, they must match :)

To fix, change your function to this:

function acceptAgreement(obj) {
  document.getElementById('<%=btnRegister.ClientID%>').disabled = !obj.checked;
}
Nick Craver
Still doesn't work :(
Ivan Stoyanov
@Ivan - What's not working, it's not toggling, it's not initially disabled, javascript error...other?
Nick Craver
the button is enabled all the time
Ivan Stoyanov
@Ivan - Are you getting a javascript error? If not, can you post the rendered markup that your page is outputting?
Nick Craver
It worked! :) I had to change OnClientClick="acceptAgreement(this)" to OnClick="acceptAgreement(this)" and set the button enabled property to false. Thank you for the help.
Ivan Stoyanov
A: 

since you have these buttons and checkbox running at server why dont you handle the enabling and disabling with the server code behind. Also which programming language are you using for the codebehind c# example

            protected void cbAcceptAgreement_CheckChanged(object sender, EventArgs e)
        {
             btnRegister.Enabled = cbAcceptAgreement.Checked;
        }

Page part

        <asp:CheckBox ID="cbAcceptAgreement" runat="server" AutoPostBack="True"
        oncheckedchanged="cbAcceptAgreement_CheckedChanged" />

lemme know if this worked for you Ivan

changed to autopostback = true for the chekcbox hopefully that will help

Laurence Burke
that's a terrible idea. doing this clientside will give much better performance, much better user experience, and with much less code (hitting the server for this would require an ajax/updatepanel implementation)
David Hedlund
I use C#. I've been searching for a code behind solution, but I couldn't find one.
Ivan Stoyanov
ok its a simple register it doesnt get used terribly often and I dont know why you would HAVE to do it client side. That may be the case only in a few examples such as mobile web browsing where u wouldnt want to wait for the server but I think in the scope of this question client side would be fine. Otherwise WHY even HAVE a client side
Laurence Burke
also what if javascript is disabled that would make the answer using javascript moot
Laurence Burke
@Laurence Burke - If javascript is disabled, your postback won't fire either, not off a checkbox :)
Nick Craver
well then will my answer work?? tell me that at least. with javascript enabled
Laurence Burke
The problem is that Visual Studio can't find .IsChecked event
Ivan Stoyanov
hold on a moment
Laurence Burke
the code is fixed now its checked not ischecked
Laurence Burke
It doesn't work
Ivan Stoyanov
@Laurence - **If** this was a good idea, at least remove the fluff :) `btnRegister.Enabled = cbAcceptAgreement.Checked;`
Nick Craver
Yeah i did it but id doesn't work
Ivan Stoyanov
ok ok ok I changed it forhopefully the last time
Laurence Burke