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
}
Regards
Gaurav Kumar

0
gkumar85
3/30/2009 5:44:48 PM
asp.net.web-forms 93655 articles. 5 followers. Follow

8 Replies
2934 Views

Similar Articles

[PageSpeed] 50

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. But you need a form to post back to the server, so what asp.net does for you is it will generate its own form called "aspnetForm".

Here is how you can do your check:


        if(window.document.getElementById('checkBox1').checked == true)
        {
            // code for checkBox1 checked
        }


        if(window.document.getElementById('checkBox2').checked == true)
        {
            // code for checkBox1 checked
        }



....
    
    
....
Sincere Regards,
dev_2580

---tollerate an enemy and love a bother---
0
dev_2580
3/30/2009 6:44:58 PM

Thanks for your reply. But it doesnt work for me. I am using following code.

function hideUserColumn() {

var checkBoxControl = document.getElementById("<%= UserCheckBox.ClientID %>")

if (checkBoxControl.checked != true) {grid.Bands[0].getColumnFromKey("User").setHidden(true);

}

else {

grid.Bands[0].getColumnFromKey("User").setHidden(false);

}

}

 But I would like to do generalization. I have to increase the checkboxes in my webform but the functionality gonna be almost same. Can I pass control name as an argument in my onclick event.

<asp:CheckBox ID="UserCheckBox" runat="server" Text="User" onclick="hideUserColumn()" />

 I would like to send some argument inside the hideUserColumn so that I can identify which checkbox was clicked. Is there any way to pass controlName as an argument into javascript function? Thanks


Regards
Gaurav Kumar

0
gkumar85
3/30/2009 7:54:14 PM

Ok, I understand what you are trying to do now. The best way that I would suggest is to create the client event from your actual code (normally in Page_Load). Create an onclick attribute for each checkbox and pass that checkbox's clientID through. ClientID ID will return the ID that the control receives after initialized, so basically whether you put it in a repeater, masterPage, placeholder... it will still find that control.

UserCheckBox.Attributes.Add("onclick", "hideUserColumn('" + UserCheckBox.ClientID + "');");
UserCheckBox2.Attributes.Add(
"onclick", "hideUserColumn('" + UserCheckBox2.ClientID + "');");

Remove the onclick from your check box because it is added in code:

<asp:CheckBox ID="UserCheckBox" runat="server" Text="User" />
<asp:CheckBox ID="UserCheckBox2" runat="server" Text="User 2" />

I sincerely hope this helps.
Sincere Regards,
dev_2580

---tollerate an enemy and love a bother---
0
dev_2580
3/31/2009 5:48:54 AM

var elementRef1 = document.getElementById('chkboxlistid');
var checkBoxArray1 = elementRef.getElementsByTagName('input');
var chkOrderBy = '';
for (var i=0; i<checkBoxArray1.length; i++)
{
var checkBoxRef1 = checkBoxArray1[i];
if (checkBoxRef1.checked == true )
{
var labelArray = checkBoxRef1.parentNode.getElementsByTagName('label');
if (labelArray.length > 0 )
{
if (chkOrderBy.length > 0 )
chkOrderBy +=
', ';
chkOrderBy += labelArray[0].innerHTML;
}
}
}


If this is a right answer to your question then,
Do Not forgot to ckick on "Mark As Answer"
0
Komal
3/31/2009 6:08:37 AM

Hi,

You can do like this -

 var ChBox1=document.getElementById('ctl00_ContentPlaceHolder1_ChBox1');

if (ChBox1.checked==true)

{

Apply you cond

}

Hope this helps You ........


Best Of Luck .........
0
Nitin
3/31/2009 6:30:14 AM

Nitin Bansal:
var ChBox1=document.getElementById('ctl00_ContentPlaceHolder1_ChBox1');

Yes, this would work well, but it is not dynamic, since if the control moves, you would have to update you javascript to prevent javascript error.
It is maybe a better practise to pass the clientID through.

 If you are not planning to make any changes, then this would be quicker.


Sincere Regards,
dev_2580

---tollerate an enemy and love a bother---
0
dev_2580
3/31/2009 7:11:42 AM
Thanks Dev. I appreciate you help.
Regards
Gaurav Kumar

0
gkumar85
3/31/2009 5:09:38 PM

using

document.getElementById('<%= ChBox1.ClientID %>'). is a better way to handle this.

at runtime <%= ChBox1.ClientID %> will be replaced by 'ctl00_ContentPlaceHolder1_ChBox1'.

This would work even if you change your ID at a later time.

 

0
ILoveFall
3/31/2009 7:11:14 PM
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"...

Javascript for validation if checkboxes are checked in web form
I have 26 checkboxes on my form. I want to use javascript to loop thru the controls and check to see if at leat 5 are checked. Any suggestions?? I don't know much about javascript. Thanks in advance!!! Hi Read on all.tag collection of the document. You can get all the "INPUT" objects of your document and check if it is of type "checkbox". To see if the checkbox is checked use the "checked" property of that object. e.g. function countCheckItems() { var items = document.body.all.tags("INPUT"); var i; var iSelec...

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

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

VS.Net Check In/Check Out Function
Hi All, I work on a team of developers who are just trying to get started with ASP.Net.  I've been doing development for 2+ years so it's not new to me but my colleagues have asked a question about the VS.Net environment that I can't answer.  We're trying to find out if there is some sort of Check In/Check Out function with Visual Studio.Net.  I've never worked in a team environment before so I couldn't answer this.  The reason we're wondering is because we're planning on setting up our online forms in a DotNetNuke style environment (one page, loads user contro...

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

Getting Error While Checking Whether CheckBox Is Checked Or Not
Hello Friends, I am getting following problem  I am using VS 2003. And VB.Net Error Message   Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error:  Line 227:            ObjCheck = CType(Me.FindControl("Customer" + I.ToString()), CheckBox)Line 228:            ObjText = CType(Me.FindControl("CustomerCode" + I.ToString()), TextBox)Line 229:  &...

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

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 out/check in
Hello How can i use the process check out/check in in PowerBuilder 8.0.3 ? We work in network. The Source Control is PB Native. I don't understand what is the parameters of Source Control ? Thank you Better to ask this in the source control group. Lots of posts on PBNative -- Evita Chapa Senior Systems Analyst II Command Technologies Inc. <Herv� Bareth> wrote in message news:3f69678e.7b3a.846930886@sybase.com... > Hello > > How can i use the process check out/check in in PowerBuilder > 8.0.3 ? We work in network. The Source Control is PB Native. ...

Checked=Checked
 I am dynamically writing out checkboxes on a page.  I have 2 link buttons on the page that sets a boolean variable to True or False. This boolen is set in Page_PreInit and is used to Select or Deselect all the Checkboxes as they are written out. A Select ALL, Deselect ALL type of thing. At least that is the plan. As the page is being written out I do this for each checkbox and then add it to a table cell control that it added to a table row and on. tc is the table cell.  Dim chkItem As CheckBox = New CheckBox chkItem.ID = "chkRecommend" + drMember("ID")...

Check in
Hi all. I am migrating from PB 7.0 to PB 10.5, After migrating my first application i am trying to work on my first window but i'am not able to find out the way to CHECK it OUT. Wher do i can find "check out/check in" functions ?? TIA Mario Parietti What source control are you using? Have you configured it? Right click on your Workspace and check the source control tab to see if you've got it set up. "Mario Parietti" <mparietti@intercad.ch> wrote in message news:451283e0$1@forums-1-dub... > Hi all. > I am migrating from PB...

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

Check In/Check out
I am using PowerBuilder 9. The undo check out is right next to the check in. This is a big problem! I so easy to select Undo check out and lose all of your changes. I lost 8 hours of work yesterday because of it. I know I should be more careful. However, there should be a "Are you sure" some where also. PLEASE FIX!!!!!!!!!!!!! -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ This isn't the tech support line, it's an end-user forum. If you want to report a bug, use CaseXpress and either open a case or report a bug ...

Web resources about - Javascript function to check whether checkbox is checked or not - asp.net.web-forms

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/11/2015 7:19:34 PM