PC Review


Reply
Thread Tools Rate Thread

How to check a checkbox using Javascript

 
 
AmitKu
Guest
Posts: n/a
 
      1st Feb 2007
<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

 
Reply With Quote
 
 
 
 
Mark Rae
Guest
Posts: n/a
 
      1st Feb 2007
"AmitKu" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

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


 
Reply With Quote
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      1st Feb 2007
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" />

--
Milosz


"AmitKu" wrote:

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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check whether user put a check in a toolbox CheckBox contro JB Microsoft ASP .NET 1 27th Aug 2009 12:18 AM
Unbound checkbox on datasheet subform - check one and they all check Robert Microsoft Access Forms 6 1st Jan 2008 05:21 PM
javascript checkbox =?Utf-8?B?cm9kY2hhcg==?= Microsoft ASP .NET 1 25th Aug 2006 07:20 AM
my javascript checkbox cannot be activated =?Utf-8?B?cGF1bCB2YW4gZ2VlcnQ=?= Microsoft Frontpage 6 28th Jul 2005 11:00 AM
checkbox and javascript DotNetJunkies User Microsoft ASP .NET 5 2nd Nov 2004 04:54 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:47 PM.