473,793 Members | 2,974 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

parent checkbox makes children checkboxes selected


I need to select children checkboxes when selecting the parent one.
This is my function:

function SelectChildrens (checkbox_name) {
form = document.forms[0];
Sname = checkbox_name.s plit("-");
for (i=0;i<form.ele ments.length;i+ +){
THATname = form.elements[i].name.split("-");
if (Sname.length== 1){
if (THATname[0]==Sname[0]){
form.elements[i].checked=!form. elements[i].checked;
}
}
if (Sname.length== 2){
if (THATname[1]==Sname[1]){
form.elements[i].checked=!form. elements[i].checked;
}
}
}//endof for
}

I've got parent checkbox which is:
<input type=checkbox name=FISH-FILTERS
onclick="Select Childrens(this. name)">
and childrens like:
<input type=checkbox name="products_ id[]" value="2">
<input type=checkbox name="products_ id[]" value="3">
<input type=checkbox name="products_ id[]" value="4">
<input type=checkbox name="products_ id[]" value="5">

what can I add to children checkboxes so that the function works fine ?

Thanks for any advices.
Kris

Jul 23 '05 #1
4 3416
fe****@multicon .pl wrote:
I need to select children checkboxes when selecting the parent one.
This is my function:

function SelectChildrens (checkbox_name) {
form = document.forms[0];
If you pass a reference to the checkbox that is clicked on it is
easier to get the form (see below).
Sname = checkbox_name.s plit("-");
I can't work out what you are trying to do here, so I've posted a
completely new function below. You have put the onclick on an
element with a name that is nothing like that of the 'childrens'
checkboxes.
for (i=0;i<form.ele ments.length;i+ +){
THATname = form.elements[i].name.split("-");
if (Sname.length== 1){
if (THATname[0]==Sname[0]){
form.elements[i].checked=!form. elements[i].checked;
}
}
if (Sname.length== 2){
if (THATname[1]==Sname[1]){
form.elements[i].checked=!form. elements[i].checked;
}
}
}//endof for
}

I've got parent checkbox which is:
<input type=checkbox name=FISH-FILTERS
onclick="Select Childrens(this. name)">
Pass a reference to the element rather than its name:

onclick="Select Childrens(this) ">
and childrens like:
<input type=checkbox name="products_ id[]" value="2">
<input type=checkbox name="products_ id[]" value="3">
<input type=checkbox name="products_ id[]" value="4">
<input type=checkbox name="products_ id[]" value="5">


Start with this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html><head><ti tle>blah</title>

<script type="text/JavaScript">

function SelectChildrens (x){
var el, els = x.form.elements ;
var m, n = x.name.split('_ ')[1];
for(var i=0, j=els.length; i<j; i++){
el = els[i];
if ('checkbox' == el.type) {
if (el.name && ( m = el.name.split(' _')[1]) && m == n ){
el.checked = x.checked;
}
}
}
}

</script>
</head>
<body>

<form action="">
<p>Selecting this checkbox will select all the other checkboxes
<br>
<input type="checkbox" name="products_ id[1]"
onclick="Select Childrens(this) ">select the 1's<br>

<input type=checkbox name="products_ id[1]" value="2">1 - 2<br>
<input type=checkbox name="products_ id[1]" value="3">1 - 3<br>

<input type="checkbox" name="products_ id[2]"
onclick="Select Childrens(this) ">select the 2's<br>

<input type=checkbox name="products_ id[2]" value="4">2 - 4<br>
<input type=checkbox name="products_ id[2]" value="5">2 - 5<br>
</p>
</form>
</body>
</html>

--
Rob
Jul 23 '05 #2
fe****@multicon .pl wrote:
I need to select children checkboxes when selecting the parent one.


I have a general-purpose reusable library that handles this, if you would
like to check it out:

http://www.javascripttoolbox.com/checkboxgroup/

It is more code than is required to handle your specific case (although it
is less than 4k), but it is more generalized and may come in handy in other
similar situations or in your current situation if the requirements change
slightly.

--
Matt Kruse
http://www.JavascriptToolbox.com
Jul 23 '05 #3
fe****@multicon .pl wrote:
I need to select children checkboxes when selecting the parent one.


Is a checkbox the intuitive device for - setting checkboxes?

Maybe yes, maybe no...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style type="text/css">

..master-before, .master-after {
width: 102px;
font: 11px tahoma;
margin: 2px 0;
}
#d1 {
width: 100px;
margin-bottom: 2px;
padding-bottom: 2px;
background: moccasin;
border: 1px black solid;
}
#d2 {
width: 100px;
background: pink;
border: 1px black solid;
}
ul {
font: 11px tahoma;
list-style-type: none;
}
input.normal {
width: 100px;
font: 11px tahoma;
text-align: center;
margin: 3px 0;
background: gainsboro;
border: 1px black solid;
}

</style>
<script type="text/javascript">

//~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ //
var gangs = [];
gangs.add = function()
{
for (var a = 0, l = arguments.lengt h; a < l; ++a)
this.push(new GangCheck(argum ents[a]));
}
gangs.init = function()
{
for (var i = 0, l = this.length, grp; i < l; ++i)
if ((grp = this[i].grp) && grp[0].onclick)
grp[0].onclick();
}
//~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ //

function GangCheck(maste rName)
{

this.set = function()
{
var el, i = 0,
bWhich = (this.value == 'check all');
while (el = this.grp[i++])
el.checked = bWhich;
this.value = bWhich ? 'uncheck all' : 'check all';
}

this.chk = function ()
{
var el, i = 0,
O_checked = true, n_checked = 0,
els = this.form.eleme nts,
grp = els[this.name];
while (el = grp[i++])
{
if (el.checked)
{
O_checked = false;
n_checked++;
}
}
if (O_checked)
this.master.val ue = 'check all';
else if (n_checked == grp.length)
this.master.val ue = 'uncheck all';
}

var f = document.forms, form,
els, el, i = 0, j, k, m;
while (form = f[i++])
{
els = form.elements;
j = -1;
while (el = els[++j])
{
if (el.name == masterName &&
(m = el.className.ma tch(/\bmaster-(.+)\b/)))
{
this.inc = (m[1] == 'before') ? 1 : -1;
this.master = el;
this.grp = els[els[j + this.inc].name];
this.master.grp = this.grp;
el.onclick = this.set;
el.disabled = false;
if (this.inc == 1)
{
k = 0;
while (el = this.master.grp[k++])
{
el.master = this.master;
el.onclick = this.chk;
}
}
else
{
k = this.master.grp .length;
while (k-- && (el = this.master.grp[k]))
{
el.master = this.master;
el.onclick = this.chk;
}
}
}
}
}
}

window.onload = function()
{
gangs.add('gang 1', 'gang2');
gangs.init();
}

</script>
</head>
<body>
<form>
<ul>
<li>
<input class="normal" type="text" name="t1" value=" Hey,">
</li><li>
<input class="normal" type="text" name="t2" value=" Lucy -">
</li><li>
<input
name="gang1"
class="master-before"
type="button"
value="check all"
disabled="disab led"
style="backgrou nd:moccasin;">
<div id="d1">
<ol>
<li>
<input type="checkbox" name="products_ id[]" value="2"> 2
</li><li>
<input type="checkbox" name="products_ id[]" value="3"> 3
</li><li>
<input type="checkbox" name="products_ id[]" value="4"> 4
</li><li>
<input type="checkbox" name="products_ id[]" value="5"> 5
</li><li>
<input type="checkbox" name="products_ id[]" value="6"> 6
</li>
</ol>
</div>
<div id="d2">
<ol style="list-style-type:lower-roman;">
<li>
<input type="checkbox" name="products_ name[]" value="foo"> foo
</li><li>
<input type="checkbox" name="products_ name[]" value="bar"> bar
</li><li>
<input type="checkbox" name="products_ name[]" value="baz"> baz
</li><li>
<input type="checkbox" name="products_ name[]" value="hah"> hah
</li><li>
<input type="checkbox" name="products_ name[]" value="feh"> feh
</li>
</ol>
</div>
<li>
<input
name="gang2"
class="master-after"
type="button"
value="check all"
disabled="disab led"
style="backgrou nd:pink;">
</li><li>
<input class="normal" type="text" name="t1" value=" I'm">
</li><li>
<input class="normal" type="text" name="t2" value=" home!">
</li>
</ul>
</form>
</body>
</html>

Jul 23 '05 #4
JRS: In article <11************ **********@g44g 2000cwa.googleg roups.com>
, dated Fri, 13 May 2005 12:59:40, seen in news:comp.lang. javascript,
RobB <fe******@hotma il.com> posted :
<style type="text/css">

.master-before, .master-after {
width: 102px;
font: 11px tahoma;
margin: 2px 0;
}
#d1 {
...


If you were to try that in a web-authoring group, I suspect that you
would be told that setting fonts to absolute sizes is BAD - and in
conflict with accessibility principles. Even if you had used 11pt.

Setting widths in px is also bad, AIUI, except for boxing graphics of
known size.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #5

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

Similar topics

4
6174
by: Shufen | last post by:
Hi, I'm a newbie that just started to learn python, html and etc. I have some questions to ask and hope that someone can help me on. I'm trying to code a python script (with HTML) to get values from a html form that consists of about 10 checkbox and a textbox where user have to key in a value to perform a search. From python tutors, I learned that I have to use the following method:
11
16489
by: totalstranger | last post by:
I have a check box with let's say 20 elements. I would like to have it align as 4 columns, 5 rows. Without using a table and using understandable CSS is there any way to make the check boxes align as I would like? I've been playing with the following selector in my attempts to get at the text portion: input + * {width:180px;} So far my attempts have produced weirdness in FF and nothing occurs in IE7. Although beyond the scope of this...
10
4492
by: LionsDome | last post by:
Hello, I have a vb.net page which a bunch of checkboxes. A user can select a checkbox(s) and hit the submit button to store those values in a SQL Server table. This works fine with no problem the first time the user submits. However when user submits a second time while changing some of the selected boxes the page only re-submits the previosly selected checkbox values. It's like its storing it somewhere in the cache or something. My...
0
3124
by: TechnoAtif | last post by:
<?php include "dbconnect.php"; include "commonFunc.php"; ?> <!----------------------------------> <table width="80%" border="1" cellpadding="2" cellspacing="0"> <tr > <td colspan="2"><p>
0
9670
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9518
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10159
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9033
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2917
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.