How to check a checkbox using Javascript

  • Thread starter Thread starter AmitKu
  • Start date Start date
A

AmitKu

<script>
function CheckChange()
{
var mycheck = document.getElementById('test');
mycheck.check = true;
}
</script>

<body>
<form id="form1" runat="server">
<asp:CheckBox ID="test" runat="server" name="test" />
<asp:CheckBox ID="CheckBox1" onclick="CheckChange()"
runat="server" />
</form>
</body>
</html>


I am trying to make it so that checking one check box checks another
using no postbacks (so basically that means using Javascript). Above
is my code, and for some reason it just doesn't work. Any ideas?

Btw instead of the above javascript, I'd rather write "test.check =
true" but that fails and Firefox's error console says "test is not
defined"

Thanks,
Amit
 
I am trying to make it so that checking one check box checks another
using no postbacks (so basically that means using Javascript). Above
is my code, and for some reason it just doesn't work. Any ideas?

The checkbox property is 'checked', not 'check' e.g. mycheck.checked = true;
 
Hi Amit,

In addition to Mark's reply use control.ClientID property, don't set name
attribute directly because it is used internally for checkbox state retrival:


<script type="text/javascript">
function CheckChange()
{
var mycheck = document.getElementById('<%=test.ClientID %>');
mycheck.checked = true;
}
</script>


<asp:CheckBox ID="test" runat="server"/>
<asp:CheckBox ID="CheckBox1" onclick="CheckChange()" runat="server" />
 
Back
Top