473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Checking selections made in a checkbox list

I have 2 datagrids on my page and each one has a template column which
has a checkbox relating to each specific row. I usually have one on
the page and use javascript to make sure there has been x number of
selections before allowing the user to proceed. I use the code below
for this:

for (var n=0; n < document.forms[0].length; n++)
{
if (document.forms[0].elements[n].type=='checkbox')
{
if (document.forms[0].elements[n].checked)
{
i++;
}
}
}

How can I make sure there has been a selection made in each of the 2
separate datagrids as my old way checks the entire page only? I am
thinking I can specifically name the datagrid I want to check but cant
remember how to do it.
Am looking for a quick solution as short on time.

Thanks.
Jul 23 '05 #1
2 2352
Menelty wrote:
I have 2 datagrids on my page and each one has a template column which
has a checkbox relating to each specific row. I usually have one on
the page and use javascript to make sure there has been x number of
selections before allowing the user to proceed. I use the code below
for this:

for (var n=0; n < document.forms[0].length; n++)
{
if (document.forms[0].elements[n].type=='checkbox')
{
if (document.forms[0].elements[n].checked)
{
i++;
}
}
}

How can I make sure there has been a selection made in each of the 2
separate datagrids as my old way checks the entire page only? I am
thinking I can specifically name the datagrid I want to check but cant
remember how to do it.
Am looking for a quick solution as short on time.

Thanks.


Name the inputs in a way that uniquely identifies them:

<input type="checkbox" name="grid1_One">
<input type="checkbox" name="grid1_Two">
<!-- etc -->
and
<input type="checkbox" name="grid2_One">
<input type="checkbox" name="grid2_Two">
<!-- etc -->

And then count each "grid#_" set separately:

function countGridsChecked() {
var f = document.forms['theForm'].elements;
var theGrids = new Object();
var reGridIdentifier = /^(grid\d+)_/;
for (var n=0; n < f.length; n++) {
if (f[n].type=='checkbox' &&
reGridIndentifier.test(f[n].name) &&
f[n].checked) {

// it's a checkbox;
// and it's name is "grid#_..."
// and it's checked

if (theGrids[RegExp.$1]) {
// counted at least one in this "grid#_..."
// increment the count
theGrids[RegExp.$1]++;
} else {
// never counted anything in this "grid#_..."
// count the first one
theGrids[RegExp.$1] = 1;
}
}

// return the object
// theGrids['grid1'], theGrids['grid2'], etc
return theGrids;
}

Now you can run:

var theCounts = countGridsChecked();
if (theCounts['grid1']) {
// the count of "grid1" is non-null and non-zero
} else {
// the count of "grid1" is null or zero
}

This scales nicely because you can add addition "grids" by simply creating
another set of checkboxes, the function itself would simply create another
property on the returned object which you could test.

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 23 '05 #2

"Menelty" <m_*******@hotmail.com> wrote in message
news:a6**************************@posting.google.c om...
I have 2 datagrids on my page and each one has a template column which
has a checkbox relating to each specific row. I usually have one on
the page and use javascript to make sure there has been x number of
selections before allowing the user to proceed. I use the code below
for this:

for (var n=0; n < document.forms[0].length; n++)
{
if (document.forms[0].elements[n].type=='checkbox')
{
if (document.forms[0].elements[n].checked)
{
i++;
}
}
}

How can I make sure there has been a selection made in each of the 2
separate datagrids as my old way checks the entire page only? I am
thinking I can specifically name the datagrid I want to check but cant
remember how to do it.
Am looking for a quick solution as short on time.

Thanks.


Give all the checkboxes in each set the same unique name, then have your
function scan for that name in all the elements of a named form.

function cbCounter(formName, rangeName)
{
var fe=document.forms[formName].elements, count=0;

for( var i=0; i<fe.length; i++)
if( fe[i].name==rangeName && fe[i].type=='checkbox' && fe[i].checked )
count++;

return count;
}

So say you had a form called "form1" containing a set of checkboxes named
"grid1", you could count the number checked with:

n = cbCounter("form1","grid1");

--
S.C.
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: RelaxoRy | last post by:
Reposted (not sure if got sent) I have results being displayed each with its own checkbox name "selected" and value "id". When someone checks a checkbox, and then continues to page through...
6
by: Joe | last post by:
I have 2 multi-list boxes, 1 displays course categories based on a table called CATEGORIES. This table has 2 fields CATEGORY_ID, CATEGORY_NAME The other multi-list box displays courses based on...
1
by: DJ Dev | last post by:
Hi, I am stuck at a problem for 3 days now. I create runtime datagrids depending on the user selections and they may vary from 2-10 depending on user selection at runtime. The datagrids are...
0
by: D. Shane Fowlkes | last post by:
OK - I'm looking for the best approach on how to do this. I have a form page where the user can edit their "Profile" (data) which is in SQL Server. It's your basic company information - address,...
0
by: D. Shane Fowlkes | last post by:
OK - I'm looking for the best approach on how to do this. I have a form page where the user can edit their "Profile" (data) which is in SQL Server. It's your basic company information - address,...
0
by: Samy | last post by:
Hi There, I have a grid view which has paging enabled in it. One of the colums is a check box and checkbox selections shows a subset of rows a user has selected from available rows. I need to...
1
by: arun.hallan | last post by:
Hi, I have two columns in a datagrid that are filled with checkboxes. I want one checkbox in a row to be checked when the corresponding checkbox is checked. I've added an OnCheckedChanged...
0
by: cvijaykrishna | last post by:
i am having a web based application and i am having a problem with it pls check it Explanationi am sending a sample code plese see it in VS-2005 FOR BETTER UNFERSTANDING I have a main page...
11
by: =?Utf-8?B?UGFyYWcgR2Fpa3dhZA==?= | last post by:
Hi All, I have a large recordset to be displayed on a ASP 3.0 page. I am using recordset paging for this. For the next and previous link i am passing href as <a href=<Page URl>?page=<%=...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.