checkbox and javascript

  • Thread starter DotNetJunkies User
  • Start date
D

DotNetJunkies User

1.
i want to populate checkboxlist using javascript only at client side ....how can i do this..by populate word i mean that checkboxes should be checked or unchecked on some condition basis....

2. after population there should be validation in checkboxlist.....
that is if user clicks a button wihout checking any one of them(i.e. at least one shuld be checked) ... alerat message shouls come on the form.......

these 2 steps should be done in javascript only(no server side programming, i know it can be done easily)
 
G

Guest

Hi,

Creating checkboxlist is very simple. All u need to do is give the same id
value for all the checkbox object.

For eg:

u have 3 checkbox and u wud like to create it as a checkboxlist,

<input type=checkbox id="chkList">CheckBox 1
<input type=checkbox id="chkList">CheckBox 2
<input type=checkbox id="chkList">CheckBox 3


The CheckBoxList is created. To access each checkbox object value, place the
following javacsript in inside ur <head> tag.

<script>
function getValues()
{
var checkBoxList = document.getElementById("chkList");

for(i=0;i<checkBoxList.length;i++)
{
if (checkBoxList .checked == true)
{
alert('CheckBox ' + i + ' is checked ')
}
}
}
</script>

Call this "getValues();" function onclick of a button.

<input type=button id="btn" onclick="getValues();">


Hope this code helps u........

Regards,
Kamal T.
 
B

bruce barker

this code will not work. id's are supposed to be unique and getElementById
only returns the first match. you can switch to usings the name attribute
and getElementsByName it will work.

<input type=checkbox name="chkList">CheckBox 1
<input type=checkbox name="chkList">CheckBox 2
<input type=checkbox name="chkList">CheckBox 3

<script>
function getValues()
{
var checkBoxList = document.getElementsByName("chkList");

for(i=0;i<checkBoxList.length;i++)
{
if (checkBoxList .checked == true)
{
alert('CheckBox ' + i + ' is checked ')
}
}
}
</script>

-- bruce (sqlwork.com)

Kamal T. said:
Hi,

Creating checkboxlist is very simple. All u need to do is give the same id
value for all the checkbox object.

For eg:

u have 3 checkbox and u wud like to create it as a checkboxlist,

<input type=checkbox id="chkList">CheckBox 1
<input type=checkbox id="chkList">CheckBox 2
<input type=checkbox id="chkList">CheckBox 3


The CheckBoxList is created. To access each checkbox object value, place the
following javacsript in inside ur <head> tag.

<script>
function getValues()
{
var checkBoxList = document.getElementById("chkList");

for(i=0;i<checkBoxList.length;i++)
{
if (checkBoxList .checked == true)
{
alert('CheckBox ' + i + ' is checked ')
}
}
}
</script>

Call this "getValues();" function onclick of a button.

<input type=button id="btn" onclick="getValues();">


Hope this code helps u........

Regards,
Kamal T.

DotNetJunkies User said:
1.
i want to populate checkboxlist using javascript only at client

side ....how can i do this..by populate word i mean that checkboxes should
be checked or unchecked on some condition basis....them(i.e. at least one shuld be checked) ... alerat message shouls come on
the form.......
 
G

Guest

when i call the above fucntion on button's click event as follwoing
---------------------------------------------------------------------------------------------
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 104px; POSITION:
absolute; TOP: 80px" runat="server"
Text="Button" OnClick="getValues()">
----------------------------------------------------------------------------------------------
then error comes as -->
----------------------------------------------------------------------------------------------
Compiler Error Message: CS0117: 'ASP.WebForm1_aspx' does not contain a
definition for 'getValues'

----------------------------------------------------------------------------------------------
apart from this , i need that if user click on button without checking any
one of them(i.e.at least one checkbox should be checked or selected by him
before he submits page to server otherwise alert message should come that
"plz check any one of them or select all of them" i.e. this problem is
something related to validation


bruce barker said:
this code will not work. id's are supposed to be unique and getElementById
only returns the first match. you can switch to usings the name attribute
and getElementsByName it will work.

<input type=checkbox name="chkList">CheckBox 1
<input type=checkbox name="chkList">CheckBox 2
<input type=checkbox name="chkList">CheckBox 3

<script>
function getValues()
{
var checkBoxList = document.getElementsByName("chkList");

for(i=0;i<checkBoxList.length;i++)
{
if (checkBoxList .checked == true)
{
alert('CheckBox ' + i + ' is checked ')
}
}
}
</script>

-- bruce (sqlwork.com)

Kamal T. said:
Hi,

Creating checkboxlist is very simple. All u need to do is give the same id
value for all the checkbox object.

For eg:

u have 3 checkbox and u wud like to create it as a checkboxlist,

<input type=checkbox id="chkList">CheckBox 1
<input type=checkbox id="chkList">CheckBox 2
<input type=checkbox id="chkList">CheckBox 3


The CheckBoxList is created. To access each checkbox object value, place the
following javacsript in inside ur <head> tag.

<script>
function getValues()
{
var checkBoxList = document.getElementById("chkList");

for(i=0;i<checkBoxList.length;i++)
{
if (checkBoxList .checked == true)
{
alert('CheckBox ' + i + ' is checked ')
}
}
}
</script>

Call this "getValues();" function onclick of a button.

<input type=button id="btn" onclick="getValues();">


Hope this code helps u........

Regards,
Kamal T.

DotNetJunkies User said:
1.
i want to populate checkboxlist using javascript only at client

side ....how can i do this..by populate word i mean that checkboxes should
be checked or unchecked on some condition basis....them(i.e. at least one shuld be checked) ... alerat message shouls come on
the form.......
 
G

Guest

ya Kamal, and Bruce ... my coe is working fine and done some modifcations in
both of urs code according to my need.. my code is following
........................
-------------------------------------------------------------------------------------------------
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="thirdpro.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<script language="javascript">
function getValues()
{
var checkBoxList1 = document.getElementById("chkList1");
var checkBoxList2 = document.getElementById("chkList2");
var checkBoxList3 = document.getElementById("chkList3");

if ((checkBoxList1.checked != true) && (checkBoxList2.checked != true) &&
(checkBoxList3.checked != true))
{
alert('Plz Select At Least One Value....');
}
else
{
alert('Crawling Processing....');
}
}

</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<input id="chkList1" type="checkbox" >CheckBox1
<input id="chkList2" type="checkbox" >CheckBox1
<input id="chkList3" type="checkbox" >CheckBox1
<input id="btn" value="Set Crawl" style="Z-INDEX: 101; LEFT: 40px;
WIDTH: 80px; POSITION: absolute; TOP: 192px; HEIGHT: 24px"
onclick="getValues()" type="button">
</form>
</body>
</HTML
-------------------------------------------------------------------------------------------------
thanks a lot for helping me ......bruce how can i become MVP (is there any
paper of microsoft for this or any other way...tell me and guide me
plz..kamal thanks ..u seems to be indian ..i m 26/m/delhi/ working in C# in a
firm whoch is not a S/W development .. im working in a team of only 3 people
so i will need urs help time to time .thanks a lot......once again..see u
soon with new probelm bye )

deepak kumar said:
when i call the above fucntion on button's click event as follwoing
---------------------------------------------------------------------------------------------
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 104px; POSITION:
absolute; TOP: 80px" runat="server"
Text="Button" OnClick="getValues()">
----------------------------------------------------------------------------------------------
then error comes as -->
----------------------------------------------------------------------------------------------
Compiler Error Message: CS0117: 'ASP.WebForm1_aspx' does not contain a
definition for 'getValues'

----------------------------------------------------------------------------------------------
apart from this , i need that if user click on button without checking any
one of them(i.e.at least one checkbox should be checked or selected by him
before he submits page to server otherwise alert message should come that
"plz check any one of them or select all of them" i.e. this problem is
something related to validation


bruce barker said:
this code will not work. id's are supposed to be unique and getElementById
only returns the first match. you can switch to usings the name attribute
and getElementsByName it will work.

<input type=checkbox name="chkList">CheckBox 1
<input type=checkbox name="chkList">CheckBox 2
<input type=checkbox name="chkList">CheckBox 3

<script>
function getValues()
{
var checkBoxList = document.getElementsByName("chkList");

for(i=0;i<checkBoxList.length;i++)
{
if (checkBoxList .checked == true)
{
alert('CheckBox ' + i + ' is checked ')
}
}
}
</script>

-- bruce (sqlwork.com)

Kamal T. said:
Hi,

Creating checkboxlist is very simple. All u need to do is give the same id
value for all the checkbox object.

For eg:

u have 3 checkbox and u wud like to create it as a checkboxlist,

<input type=checkbox id="chkList">CheckBox 1
<input type=checkbox id="chkList">CheckBox 2
<input type=checkbox id="chkList">CheckBox 3


The CheckBoxList is created. To access each checkbox object value, place the
following javacsript in inside ur <head> tag.

<script>
function getValues()
{
var checkBoxList = document.getElementById("chkList");

for(i=0;i<checkBoxList.length;i++)
{
if (checkBoxList .checked == true)
{
alert('CheckBox ' + i + ' is checked ')
}
}
}
</script>

Call this "getValues();" function onclick of a button.

<input type=button id="btn" onclick="getValues();">


Hope this code helps u........

Regards,
Kamal T.

:

1.
i want to populate checkboxlist using javascript only at client

side ....how can i do this..by populate word i mean that checkboxes should
be checked or unchecked on some condition basis....
2. after population there should be validation in checkboxlist.....
that is if user clicks a button wihout checking any one of
them(i.e. at least one shuld be checked) ... alerat message shouls come on
the form.......
these 2 steps should be done in javascript only(no server side
programming, i know it can be done easily)engine supports Post Alerts, Ratings, and Searching.
 
G

Guest

bruce is right...... it shld. be name attribute instead of id

Kamal T. said:
Hi,

Creating checkboxlist is very simple. All u need to do is give the same id
value for all the checkbox object.

For eg:

u have 3 checkbox and u wud like to create it as a checkboxlist,

<input type=checkbox id="chkList">CheckBox 1
<input type=checkbox id="chkList">CheckBox 2
<input type=checkbox id="chkList">CheckBox 3


The CheckBoxList is created. To access each checkbox object value, place the
following javacsript in inside ur <head> tag.

<script>
function getValues()
{
var checkBoxList = document.getElementById("chkList");

for(i=0;i<checkBoxList.length;i++)
{
if (checkBoxList .checked == true)
{
alert('CheckBox ' + i + ' is checked ')
}
}
}
</script>

Call this "getValues();" function onclick of a button.

<input type=button id="btn" onclick="getValues();">


Hope this code helps u........

Regards,
Kamal T.

DotNetJunkies User said:
1.
i want to populate checkboxlist using javascript only at client side ....how can i do this..by populate word i mean that checkboxes should be checked or unchecked on some condition basis....

2. after population there should be validation in checkboxlist.....
that is if user clicks a button wihout checking any one of them(i.e. at least one shuld be checked) ... alerat message shouls come on the form.......

these 2 steps should be done in javascript only(no server side programming, i know it can be done easily)
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top