how to check if a checkbox is checked using javascript.

Hi all, I am new to asp.net and trying to learn it,

I have created a simple page including a button and a checkboxes.

 Now, I want that when I check the checkbox, it should display a message. but it is gving some errors like

1. ) expected

2. Invalid expresson term ':'

3. ; expexcted 

4. Invalid expresson term ':'

 my sample coding is as follows

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

<script type="text/javascript">

function validate(checkbox)

{

alert(checkbox.checked);

return;

}

</script>

</head>

<body>

<form id="form1" runat="server">

<div>

&nbsp;<asp:Button ID="Button1" runat="server" Text="Button" />

<asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="javascript:return validate(this);"/></div>

</form>

</body>

</html>

 

 

0
aakashagarwal
6/17/2008 7:46:16 AM
asp.net.client-side 24353 articles. 1 followers. Follow

8 Replies
664 Views

Similar Articles

[PageSpeed] 20

The problem is that OnCheckedChanged refer to a server metod, i.e. the event will trigger a postback to the server and that method will be executed.

You need to hook up a client-side event. Add this line in your Page_Load:

CheckBox1.Attributes.Add("onclick", "return validate(this)");
  

If this post was useful to you, please mark it as answer. Thank you!
0
johram
6/17/2008 8:17:31 AM

hi Akash try like this

function validate(checkbox)

{

alert(document.getElementById(checkbox).checked);

return;

}

0
sivakl_2001
6/17/2008 8:18:25 AM

try this

function validate(checkbox)

{

alert(document.getElementById('<%= CheckBox1.ClientID  %>' ).checked);

return;

}

0
sivakl_2001
6/17/2008 8:20:34 AM

 

sivakl_2001:

try this

function validate(checkbox)

{

alert(document.getElementById('<%= CheckBox1.ClientID  %>' ).checked);

return;

}

This is not really the problem, as he already passes this to the function. This will evaluate to the checkbox control where the event was raised, so it's fine.


If this post was useful to you, please mark it as answer. Thank you!
0
johram
6/17/2008 8:21:50 AM

oops sory

<asp:CheckBox ID="CheckBox1" onclick="validate(this)"  runat="server" "/>

0
sivakl_2001
6/17/2008 8:23:58 AM

sivakl_2001:

oops sory

<asp:CheckBox ID="CheckBox1" onclick="validate(this)"  runat="server" "/>

 

Sorry, but onclick refers to a server event which is really the basic problem here. So the event is not fired on the client. You need to use the Attributes construct to make this work.


If this post was useful to you, please mark it as answer. Thank you!
0
johram
6/17/2008 8:30:33 AM

johram:

sivakl_2001:

oops sory

<asp:CheckBox ID="CheckBox1" onclick="validate(this)"  runat="server" "/>

 

Sorry, but onclick refers to a server event which is really the basic problem here. So the event is not fired on the client. You need to use the Attributes construct to make this work.

Jorom its working for me

function validate()

{

alert(document.getElementById('<%= CheckBox1.ClientID %>').checked);

return;

}

<asp:CheckBox ID="CheckBox1" onclick="validate()" runat="server" />

0
sivakl_2001
6/17/2008 8:38:18 AM

Sorry, my bad. I was confusing the asp:Checkbox with winforms:Checkbox. As you've shown, adding a handler for OnClick will indeed work, although not recommended as it is not a property of asp:CheckBox, it's a property of the native checkbox form element. Thus you will get a warning when you compile the code, but it will work. The "right" way to do it is to use the Attributes collection, as it will not cause any markup conflicts.

The same goes for many other asp controls. You can, for example set class="something" on your control (for CSS markup), although not supported. ASP will pass this along to the page with a warning and it will work in the end. The right way to do the CSS class thing is to use the CssClass property of the asp control.


If this post was useful to you, please mark it as answer. Thank you!
0
johram
6/17/2008 8:59:30 AM
Reply:

Similar Artilces:

checking checkbox is checked or not using checkbox.checked
Hi,my checkbox is<asp:CheckBox ID="Chkcity" runat="server" Text="My own city"  OnCheckedChanged="EventHandler"/>code behind: Private Sub Chkcity_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Chkcity.CheckedChangedif (Chkcity ...............)end if End SubERROR    'EventHandler' is not a member of 'ASP.bpoaddjob_aspx'.    Please Mark the post as "ANSWER" if it helps you I think it should be like this:<asp:CheckBox ID="Chkcity"...

Checking All CheckBoxes in a GridView Using Client-Side Script
Hi; I have  checkbox column in gridview and a checkbox in header gridview. i write down code: Dim checkbox As CheckBox = GridView2.HeaderRow.FindControl("HeaderCheckbox")checkbox.Attributes("onclick") = "ChangeAllCheckBoxStates(this.checked);"  For Each row As GridViewRow In GridView2.Rows Dim a As CheckBox = row.Cells(0).FindControl("CheckBox2") If row.RowType = DataControlRowType.DataRow ThenClientScript.RegisterArrayDeclaration("CheckboxIDs", String.Concat("'", a.ClientID, "'")) End IfNe...

Use JavaScript to check if CSS is enabled client-side
Hi all, As the subject suggests, I would like to find out if there is a way I can use JavaScript to check if CSS is enabled in the user's browser.This should be done upon page load, and would be handy if some kind of notification could be used. Maybe a label...?If anybody could provide some code examples, it would be much appreciated.  Thanks for any help in advance.   Try this: http://www.filamentgroup.com/lab/delivering_the_right_experience_to_the_right_device/...

javascript: check all check box in a perticular row , when i check a checkbox i that perticuler row
Hi Friends,I have a gridview. and I am having checkboxes in all the columns and all the rows. Like it is about selecting subjects.My Problem is when I select a Checkbox in First column of any row, all the checkboxes in that particular row should also be selected.How can I do this... . I have done all checkboxes checked for column, but no idea about row.Please guide.Thanks. Hi, this can be done with the help of jQuery, a great & free javascript library. Take a look at this article: Check All Checkboxes in GridView using JQuery. Grz, Kris.Read my blog. Handy Firefox plugins for web dev...

Check All Check Box While Check Header CheckBox
 hello...I have two grid on same page...both grid contains header checkbox and item checkbox i want that if use check header checkbox then all item check box checked,i have javascript for this functinality but problem is that when i click on header check box of first grid then checkbox of second grid also checked that i dnt want i want to perform this operation on one grid at one time.i have attache my script also...plz help me....thanks<script type="text/javascript">        function SelectAll(ID)      &nbs...

Changing the backcolor once i check the checkbox in checkbox list using javascript
Hi Friends, My requirement is Listbox with checkbox I have taken one div tag make it as overlap.in tat div tag i hav taken one checkboxlist.. Now it looks like a listbox with checkox. Data is binded properly.once i check the checkbox in checkboxlist  i need to change tat checkbox row color using javascript . Please can u give some suggestions Thanks sandhya   These links should Help http://www.gridviewguy.com/ArticleDetails.aspx?articleID=141_Changing_GridView_Row_Color http://forums.asp.net/t/1166853.aspxVikram www.vikramlakhotia.comPlease mark the answer if ...

Javascript function to check whether checkbox is checked or not
I have 2 check boxes on my form and would like to do create javascript function to check which checkbox is clicked and whether its checked or unchecked. I am using master page also so unable to do like this:- if(document.theForm.CheckBox1.checked==false) { //Some processing }RegardsGaurav Kumar You can do this with your javascript, it is better than document.theForm.CheckBox1... because it adheres to the w3c standards. The reason that document.theForm.CheckBox1... will not work is because when you use a master page, you do not place you cannot create a form to place your code in. ...

Check One CheckBox To Check All CheckBoxes
All the rows in a DataGrid, including the Header, are accompanied with a CheckBox. I want that when the CheckBox in the Header is checked, then all the CheckBoxes should automatically get checked. I set the AutoPostBack property of the CheckBox in the Header to True & am invoking a sub named CheckAllRows on the CheckedChanged event of this CheckBox. The CheckBox in the Header exists within the HeaderTemplate of a TemplateColumn in the DataGrid & the rest of the CheckBoxes reside within the ItemTemplate of the same TemplateColumn. This is the code (the ID of the CheckBox in the Header...

How to use client-side script(javascript) in DNA using .Net Remoting
Hi all,     It was known that SAO and Singleton object about .Net Remoting. When I Create an object in server side and hosting in IIS's virtual directory,I want to use javascript asynchronous invoking the remote object by HTTP channel, 80 port and SOAP formatter just like XMLHTTP invoking web service. My code as following, <script> objXmlDoc = new ActiveXObject("Msxml2.DOMDocument"); objHttp = new ActiveXObject("Msxml2.XMLHTTP"); strEnvelope = ?? objHttp.onreadystatechange = function() { ...... }   szUrl = "http://ServerNa...

using Javascript to check a checkbox?
I need to check a checkbox server control using javascript when a button is clicked and yes it does have to be done in Javascript. Anyone know the syntax? thanx weisenbr You can write like this in the onclick function of the button: checks = document.getElementsByTagName("input") for (i=1;i<=checks.length;i++) { if (checks[i-1].type == "checkbox") { if ( checks[i-1].checked) { // put your logic here ...

Checkbox checked at client side???
Hi, I have a datagrid. In this datagrid At each row I have a checkbox to select the whole row. The second column is a bound column and it keeps the id of the product, the third contains the name of the product and so on... I want to get the id of the product as soon as i check the checkbox. However,this should be handled in client side. Can anyone help me? Thanks... Implement the ItemDataBound event of the grid and make sure ID of the row gets passed to the client side onclick event handler of the checkbox.Bug [MCSD] I'm sorry but I could not manage to implement ItemDataB...

How to check for the existing user in database and using custom validator how to check check box is checked or not?
Hi Friends, For Registration page i want to check if that user already exist in database or not? and i also have to check if user have check the checkbox for i agree then only he will be able to complete registration. here i am sending my design. and all validation i have done all validation except these two.<html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>PRIZE MLS</title><link href="css/style.c...

Javascript validation on gridviev for checking wheter any of the checkbox is checked or not
Hi guys! I have gridview with checkboxes.  I have calculate button outside gridview. This calculate button do some calculations depending on the row selected by checking checkbox Now on onClientClick event of calculate button i want to call javascript which will show alert message if none of the row is selected by checking checkbox. how to achieve  this...... Any help will be appreciated.   Thank yousarvesh button OnClientClick = "return ValidateCheckBox();"function ValidateCheckBox(){var allChkBox   = document.getElementsByTagName('input&...

Check In Check Out
I have some source code that I need to modify and it was sent to me in a Registered status. Therfore, when I open any object it says " 'object name' is registered but not checked out and therefore can not be overwritten. Open anyway?" Then when I try to check it out, I get "PBNative checkout not allowed for registered objects." How can I get around this? When I look under the "Source" menu, all the items concerning registered are grayed out. TIA Option1:Copy the object(Only) to someother pbl and try . Option2:If You have any SCC Expo...

Web resources about - how to check if a checkbox is checked using javascript. - asp.net.client-side

JavaScript Kit- Your comprehensive JavaScript, DHTML, CSS, and Ajax stop
Your comprehensive JavaScript, DHTML, CSS, and Ajax stop

Let's Code: Test-Driven Javascript by James Shore — Kickstarter
James Shore is raising funds for Let's Code: Test-Driven Javascript on Kickstarter! A screencast series focusing on rigorous, professional Javascript ...

Eloquent JavaScript: A Modern Introduction to Programming
... and pragmatics. I loved the tutorial-style game-like program development. This book rekindled my earliest joys of programming. Plus, JavaScript! ...

Lightbox (JavaScript) - Wikipedia, the free encyclopedia
On a Lightbox-enabled page, a user can click an image to have it magnified in a Lightbox window, which resizes itself according to the size of ...

Open Directory - Computers: Programming: Languages: JavaScript: Tools: Obfuscators
about dmoz - help Top : Computers : Programming : Languages : JavaScript : Tools : Obfuscators Tools for obfuscation/compression JavaScript code. ...

Facebook File-Transfer App Pipe Relaunches, Now Works Via JavaScript, WebRTC
... allowed Facebook users to send and receive files up to 1 gigabit in size. A new version of Pipe debuted Monday, and the app now works via JavaScript ...

Under the Hood: The JavaScript SDK - The use of polyfills - Facebook
Facebook Engineering hat eine Notiz mit dem Titel Under the Hood: The JavaScript SDK - The use of polyfills geschrieben. Du kannst den vollständigen ...

JavaScript SDK - Facebook-Entwickler
Overview The JavaScript SDK provides a rich set of client-side functionality for accessing Facebook...

Senior Front-End Developer (Javascript and AJAX)
Company: Sometrics Category: Software Development Location: Los Angeles, CA How to Apply: jobs@sometrics.com Description: We are looking for ...

Badass JavaScript (@badass_js) on Twitter
WebGLWorker is an open source library that allows using WebGL from Web Workers. Great work as always from @kripken . blog.mozilla.org/research/2014/… ...

Resources last updated: 12/27/2015 11:22:57 AM