How to check a checkbox using Javascript

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
 
M

Mark Rae

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

Guest

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top