473,320 Members | 1,722 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,320 software developers and data experts.

How to check all the checkboxes if checkbox name is 'name[]'

Hi, since sometime I'm stuck in a problem where I want to check or
uncheck all the checkboxes. If I'm choosing name for the checkbox
array as 'chkbx_ary' then I'm able to check/uncheck all the checkboxes
(code pasted below). But if name of the checkbox array is
'chkbx_ary[]' then it's failing. I want the name to be 'chkbx_ary[]'
because I want to access this array at server side.
Though one may not require to see php part but I'm still pasting the
code.

<script language="JavaScript" type="text/javascript">
function CheckAll(field)
{
for (loop=0; loop < field.length; loop++)
{
field[loop].checked = true;
}
}

function UnCheckAll(field)
{
for (loop=0; loop < field.length; loop++)
{
field[loop].checked = false;
}
}
</script>

<?php
echo '<form name="TestSelect_Form" method="GET" action="TestList.php"
enctype="text/plain" style="margin:0px">
<input type="radio" name="SelectOrNot" value="Select_All"
onClick="CheckAll(document.TestSelect_Form.SelectM odule)"
style="position:absolute;left:290px;top:140px;z-index:6">
<input type="radio" name="SelectOrNot" value="Uncheck_All"
onClick="UnCheckAll(document.TestSelect_Form.Selec tModule)" checked
style="position:absolute;left:400px;top:140px;z-index:7">';
$file_name = "CONF/testselect.txt";
$fp_TstSlct = fopen("$file_name", "r") or die("Some error occurred
while opening 'testSelect.txt");
$TopPos = 170;
while(!feof($fp_TstSlct))
{
$TstSlct_data = fgets($fp_TstSlct);
preg_match_all("/([#]*)(.*)\s(\d+)\s*[;#]*(.*)/", $TstSlct_data,
$match, PREG_PATTERN_ORDER);
if ( $match[2][0] != NULL ) { //Checking whether module is present
or not. May be only comments have been put. For. e.g.
###################DSL HOME#########
if ( $match[1][0] == NULL ) { //Checking if line is starting with
'#' or not
echo '<input type="checkbox" name="SelectModule[]" value=' .
$match[2][0] . ' checked="checked" style="position:absolute;left:
90px;top:' . $TopPos . 'px">';
} else {
echo '<input type="checkbox" name="SelectModule[]" value=' .
$match[2][0] . ' style="position:absolute;left:90px;top:' . $TopPos .
'px">';
}

echo '<div id="text3" style="position:absolute; overflow:hidden;
left:130px; top:' . $TopPos . 'px; width:79px; height:16px"><div
class="wpmd">';
echo '<div><font face="Arial">' . $match[2][0] . '</font></div>';
echo '</div></div>';

$TopPos = $TopPos + 25;
}
}
fclose($fp_TstSlct);

echo '<input name="ConfigureTestList" type="submit" value="Next"
style="position:absolute;left:326px;top:403px;z-index:16">
</form>

<div id="text1" style="position:absolute; overflow:hidden; left:
265px; top:118px; width:69px; height:18px; z-index:9"><div
class="wpmd">
<div><font class="ws12" face="Arial">Select All</font></div>
</div></div>

<div id="text2" style="position:absolute; overflow:hidden; left:
371px; top:118px; width:85px; height:18px; z-index:10"><div
class="wpmd">
<div><font class="ws12" face="Arial">Uncheck All</font></div>
</div></div>';
?>

Please someone suggest the solution as I'm totally stuck.
Thanks a lot in advance.
Oct 28 '08 #1
13 3030
PhpCool wrote:
Hi, since sometime I'm stuck in a problem where I want to check or
uncheck all the checkboxes. If I'm choosing name for the checkbox
array as 'chkbx_ary' then I'm able to check/uncheck all the checkboxes
(code pasted below). But if name of the checkbox array is
'chkbx_ary[]' then it's failing. I want the name to be 'chkbx_ary[]'
because I want to access this array at server side.
Though one may not require to see php part but I'm still pasting the
code.
How about?
obj = document.getElementsByName('chkbx_ary[]'); obj[i].value=something;

I know this works because in one of my apps I have "n" similar rows, all
with checkboxes named cond[] (for the same reason -- php). I have a
clear button at the end of each row where I call a javascript function
passing in the id number, i. In that function I use:

obj = document.getElementsByName('cond[]'); obj[i].value=0;

It should be a simple matter for you to do:

obj = document.getElementsByName('chkbx_ary[]');
for (i=0; i<obj.length; i++) {
obj[i].value = something;
}
Oct 28 '08 #2
PhpCool wrote:
If I'm choosing name for the checkbox
array as 'chkbx_ary' then I'm able to check/uncheck all the checkboxes
(code pasted below). But if name of the checkbox array is
'chkbx_ary[]' then it's failing. I want the name to be 'chkbx_ary[]'
because I want to access this array at server side.
The characters [ and ] aren't valid in a name. You can use a-z, A-Z and
0-9 and underscore. That's it.
Oct 28 '08 #3
On 2008-10-28 13:29, Stevo wrote:
The characters [ and ] aren't valid in a name. You can use a-z, A-Z and
0-9 and underscore. That's it.
If you're talking about the HTML "name" attribute, that's not correct.
The content of "name" is defined as cdata, which includes the square
brackets. <input name="box[]" type="checkbox" value="foo"is valid.

If you're talking about ECMAScript identifiers, it's not correct either.
According to the standard, almost any Unicode letter (or corresponding
escape sequence) can be used in an Identifier:

Identifier ::
IdentifierName but not ReservedWord

IdentifierName ::
IdentifierStart
IdentifierName IdentifierPart

IdentifierStart ::
UnicodeLetter
$
_
\ UnicodeEscapeSequence

UnicodeLetter
any character in the Unicode categories “Uppercase letter (Lu)”,
“Lowercase letter (Ll)”, “Titlecase letter (Lt)”,
“Modifier letter (Lm)”, “Other letter (Lo)”, or “Letter number
(Nl)”.
This is valid ECMAScript:

var привет = "hello";
alert(привет);

I found that rather surprising, but it appears to work at least in
Firefox and Opera, and in Rhino:

jsvar привет = "hello";
jsprint(привет);
hello

It does not work with the SpiderMonkey jsshell:

jsvar привет = "hello";
typein:1: SyntaxError: illegal character:
typein:1: var привет = "hello";
typein:1: .....^
jsvar hélłö = "hello";
typein:2: SyntaxError: illegal character:
typein:2: var hélłö = "hello";
typein:2: ......^
So, all in all it's probably not a good idea to rely on it, and stick to
ASCII characters in identifiers.
- Conrad
Oct 28 '08 #4
On Oct 28, 7:29*am, Stevo <n...@mail.invalidwrote:
The characters [ and ] aren't valid in a name. You can use a-z, A-Z and
0-9 and underscore. That's it.
That's an old misunderstanding, dis-proven many times. Please don't
keep spreading it.

Matt Kruse
Oct 28 '08 #5
Conrad Lender wrote:
On 2008-10-28 13:29, Stevo wrote:
>The characters [ and ] aren't valid in a name. You can use a-z, A-Z and
0-9 and underscore. That's it.

[...]
If you're talking about ECMAScript identifiers, it's not correct either.
According to the standard, almost any Unicode letter (or corresponding
escape sequence) can be used in an Identifier:
That's nonsense, at best misleading.
Identifier ::
IdentifierName but not ReservedWord

IdentifierName ::
IdentifierStart
IdentifierName IdentifierPart

IdentifierStart ::
UnicodeLetter
$
_
\ UnicodeEscapeSequence

UnicodeLetter
any character in the Unicode categories “Uppercase letter (Lu)”,
“Lowercase letter (Ll)”, “Titlecase letter (Lt)”,
“Modifier letter (Lm)”, “Other letter (Lo)”, or “Letter number
(Nl)”.
Note that "[" and "]" do _not_ occur and are _not_ contained in the
mentioned Unicode subsets; only letters are allowed for good reason. You
may include these characters in identifiers escaped as \u005b and \u005d,
but not as they are. A wise design decision, otherwise

f[oo]][][][b][a][r][]]

would confuse the hell out of the parser ;-)
This is valid ECMAScript:

var привет = "hello";
alert(привет);

I found that rather surprising, but it appears to work at least in
Firefox and Opera, and in Rhino:
[...]
It does not work with the SpiderMonkey jsshell:

jsvar привет = "hello";
typein:1: SyntaxError: illegal character:
typein:1: var привет = "hello";
typein:1: .....^
[...]
So, all in all it's probably not a good idea to rely on it,
It *might not* be a good idea to rely on it, especially if older
implementations such as JavaScript 1.2/1.3 of Netscape 4.x should be
considered. Certainly your SpiderMonkey build not having Unicode support
compiled in (for whatever reason) provides no valid argument at all in favor
of avoiding Unicode identifiers. Obviously it is compiled into the version
implemented in Gecko.
and stick to ASCII characters in identifiers.
A property *name* does not need to be an identifier:

var foo = {"bar[]": "baz"};
foo["bar[]"] = 42;

Hint: `this' can be the base object of a property access.
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Oct 29 '08 #6
Stevo wrote:
PhpCool wrote:
>If I'm choosing name for the checkbox
array as 'chkbx_ary' then I'm able to check/uncheck all the checkboxes
(code pasted below). But if name of the checkbox array is
'chkbx_ary[]' then it's failing. I want the name to be 'chkbx_ary[]'
because I want to access this array at server side.

The characters [ and ] aren't valid in a name. You can use a-z, A-Z and
0-9 and underscore. That's it.
....as I said, I used cond[] as a name and it worked perfectly for me.
Oct 29 '08 #7
On 2008-10-29 02:13, Thomas 'PointedEars' Lahn wrote:
>According to the standard, almost any Unicode letter (or
corresponding escape sequence) can be used in an Identifier:

That's nonsense, at best misleading.
No, it's correct. Please re-read what I actually wrote. I said "almost
any Unicode *letter*", not character. There's a difference.
>UnicodeLetter
any character in the Unicode categories “Uppercase letter (Lu)”,
“Lowercase letter (Ll)”, “Titlecase letter (Lt)”,
“Modifier letter (Lm)”, “Other letter (Lo)”, or “Letter number
(Nl)”.

Note that "[" and "]" do _not_ occur and are _not_ contained in the
mentioned Unicode subsets;
I never said they were.
>So, all in all it's probably not a good idea to rely on it,

It *might not* be a good idea to rely on it, especially if older
implementations such as JavaScript 1.2/1.3 of Netscape 4.x should be
considered. Certainly your SpiderMonkey build not having Unicode
support compiled in (for whatever reason) provides no valid argument
at all in favor of avoiding Unicode identifiers. Obviously it is
compiled into the version implemented in Gecko.
Yeah, I had some trouble compiling it, and you're right, the quick test
with jsshell didn't prove much. I only tried it out of curiosity (and
because I was too lazy to start the Windows VM for IE and Safari).

[a little OT SpiderMonkey detour] The default compile went without a
hitch, but when I tried to use different configurations (like adding
support for the old File object), it always failed. The suggestions I
found in m.d.t.js-engine didn't work for me, but I found out that the
stand-alone SpiderMonkey interpreter probably wasn't what I was looking
for anyway. Somebody suggested JSDB, which embeds the SpiderMonkey
engine and comes with an assortment of useful host objects. I asked
about that here a few days ago, but haven't found the time to try it out
yet. [end detour :-)]
A property *name* does not need to be an identifier:

var foo = {"bar[]": "baz"}; foo["bar[]"] = 42;

Hint: `this' can be the base object of a property access.
Thanks, I'm aware of that, and I was specifically talking about
identifiers. But the OP will be interested in this snippet.
- Conrad
PS: While I'm already off-topic, could I ask you a personal favor? You
have a habit of starting your replies with "Nonsense." I personally find
it very rude to summarily dismiss another poster's opinion as nonsense.
It comes very close to an insult. I don't know if this is some kind of
language-barrier thing (unlikely because your English is practically
indistinguishable from a native speaker; but it's possible). I like
talking to you, and I value your insightful comments. I've made my share
of mistakes, as have we all, and I'm always grateful for corrections,
but your "Nonsense" tags only serve to make me defensive and angry. I
wish you could find a different, more polite form of expressing your
disagreement - especially when you see that the person you're replying
to has actually tried to make a logical argument. Sure, I can "stand the
heat", as you've described it before. I just don't see any reason for
unprovoked fire and flames in a technical discussion. It's your choice,
of course, but you should be aware of how it will be received.
Oct 29 '08 #8
On Oct 28, 10:59*pm, Conrad Lender <crlen...@yahoo.comwrote:
On 2008-10-29 02:13, Thomas 'PointedEars' Lahn wrote:
[snip]
>
PS: While I'm already off-topic, could I ask you a personal favor? You
You should have done that by email.
have a habit of starting your replies with "Nonsense." I personally find
it very rude to summarily dismiss another poster's opinion as nonsense.
It comes very close to an insult. I don't know if this is some kind of
Personally, I don't condone such behavior, but more often than not, he
is calling a spade a spade.
Oct 29 '08 #9
Thomas 'PointedEars' Lahn wrote:
Conrad Lender wrote:
>On 2008-10-28 13:29, Stevo wrote:
>>The characters [ and ] aren't valid in a name. You can use a-z, A-Z and
0-9 and underscore. That's it.
[...]
If you're talking about ECMAScript identifiers, it's not correct either.
According to the standard, almost any Unicode letter (or corresponding
escape sequence) can be used in an Identifier:

That's nonsense, at best misleading.
Oops: *letter*. Sorry, so that's not nonsense; but it is misleading because
identifiers are not names.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Oct 29 '08 #10
On Oct 28, 4:28*pm, PhpCool <goyal.anujku...@gmail.comwrote:
Hi, since sometime I'm stuck in a problem where I want to check or
uncheck all the checkboxes. If I'm choosing name for the checkbox
array as 'chkbx_ary' then I'm able to check/uncheck all the checkboxes
(code pasted below). But if name of the checkbox array is
'chkbx_ary[]' then it's failing. I want the name to be 'chkbx_ary[]'
because I want to access this array at server side.
Though one may not require to see php part but I'm still pasting the
code.

<script language="JavaScript" type="text/javascript">
* * * * function CheckAll(field)
* * * * {
* * * * * * * * for (loop=0; loop < field.length; loop++)
* * * * * * * * {
* * * * * * * * * * * * field[loop].checked = true;
* * * * * * * * }
* * * * }

* * * * function UnCheckAll(field)
* * * * {
* * * * * * * * for (loop=0; loop < field.length; loop++)
* * * * * * * * {
* * * * * * * * * * * * field[loop].checked = false;
* * * * * * * * }
* * * * }
</script>

<?php
* * * * echo '<form name="TestSelect_Form" method="GET" action="TestList.php"
enctype="text/plain" style="margin:0px">
* * * * * * * * <input type="radio" name="SelectOrNot" value="Select_All"
onClick="CheckAll(document.TestSelect_Form.SelectM odule)"
style="position:absolute;left:290px;top:140px;z-index:6">
* * * * * * * * <input type="radio" name="SelectOrNot" value="Uncheck_All"
onClick="UnCheckAll(document.TestSelect_Form.Selec tModule)" checked
style="position:absolute;left:400px;top:140px;z-index:7">';
* * * * $file_name = "CONF/testselect.txt";
* * * * $fp_TstSlct = fopen("$file_name", "r") or die("Some error occurred
while opening 'testSelect.txt");
* * * * $TopPos = 170;
* * * * while(!feof($fp_TstSlct))
* * * * {
* * * * * * * * $TstSlct_data = fgets($fp_TstSlct);
* * * * * * * * preg_match_all("/([#]*)(.*)\s(\d+)\s*[;#]*(.*)/", $TstSlct_data,
$match, PREG_PATTERN_ORDER);
* * * * * * * * if ( $match[2][0] != NULL ) { * //Checking whether module is present
or not. May be only comments have been put. For. e.g.
###################DSL HOME#########
* * * * * * * * * * * * if ( $match[1][0] == NULL ) { * //Checking if line is starting with
'#' or not
* * * * * * * * * * * * * * * * echo '<input type="checkbox" name="SelectModule[]" value=' .
$match[2][0] . ' checked="checked" style="position:absolute;left:
90px;top:' . $TopPos . 'px">';
* * * * * * * * * * * * } else {
* * * * * * * * * * * * * * * * echo '<input type="checkbox" name="SelectModule[]" value=' .
$match[2][0] . ' style="position:absolute;left:90px;top:' . $TopPos .
'px">';
* * * * * * * * * * * * }

* * * * * * * * * * * * echo '<div id="text3" style="position:absolute; overflow:hidden;
left:130px; top:' . $TopPos . 'px; width:79px; height:16px"><div
class="wpmd">';
* * * * * * * * * * * * echo '<div><font face="Arial">' . $match[2][0] . '</font></div>';
* * * * * * * * * * * * echo '</div></div>';

* * * * * * * * * * * * $TopPos = $TopPos + 25;
* * * * * * * * }
* * * * }
* * * * fclose($fp_TstSlct);

* * * * echo '<input name="ConfigureTestList" type="submit" value="Next"
style="position:absolute;left:326px;top:403px;z-index:16">
* * * * </form>

* * * * <div id="text1" style="position:absolute; overflow:hidden; left:
265px; top:118px; width:69px; height:18px; z-index:9"><div
class="wpmd">
* * * * <div><font class="ws12" face="Arial">Select All</font></div>
* * * * </div></div>

* * * * <div id="text2" style="position:absolute; overflow:hidden; left:
371px; top:118px; width:85px; height:18px; z-index:10"><div
class="wpmd">
* * * * <div><font class="ws12" face="Arial">Uncheck All</font></div>
* * * * </div></div>';
?>

Please someone suggest the solution as I'm totally stuck.
Thanks a lot in advance.
Hello PhpCool.

I've modified your code, and here is the updated version..

#####
<script language="JavaScript" type="text/javascript">
function checkAll (rad, selectType) {
for (var i=0;i < document.TestSelect_Form.elements.length;i++) {
var e = document.TestSelect_Form.elements[i];
if ('checkbox' == e.type && 'select' == selectType) {
e.checked = true;
} else {
e.checked = false;
}
}
rad.checked = true;
}
</script>

<?php
echo '<form name="TestSelect_Form" method="GET"
action="TestList.php" enctype="text/plain" style="margin:0px">
<input type="radio" name="SelectOrNot" value="Select_All"
onClick="checkAll(this, \'select\');" style="position:absolute;left:
290px;top:140px;z-index:6">
<input type="radio" name="SelectOrNot" value="Uncheck_All"
onClick="checkAll(this, \'unselect\');" checked
style="position:absolute;left:400px;top:140px;z-index:7">';
#####

Hope this code will work for you.
Let me know.

Enjoy.
Oct 29 '08 #11
On Oct 28, 12:28*pm, PhpCool <goyal.anujku...@gmail.comwrote:
Hi, since sometime I'm stuck in a problem where I want to check or
uncheck all the checkboxes. If I'm choosing name for the checkbox
array as 'chkbx_ary' then I'm able to check/uncheck all the checkboxes
(code pasted below). But if name of the checkbox array is
'chkbx_ary[]' then it's failing. I want the name to be 'chkbx_ary[]'
because I want to access this array at server side.
Though one may not require to see php part but I'm still pasting the
code.

<script language="JavaScript" type="text/javascript">
* * * * function CheckAll(field)
* * * * {
* * * * * * * * for (loop=0; loop < field.length; loop++)
* * * * * * * * {
* * * * * * * * * * * * field[loop].checked = true;
* * * * * * * * }
* * * * }

* * * * function UnCheckAll(field)
* * * * {
* * * * * * * * for (loop=0; loop < field.length; loop++)
* * * * * * * * {
* * * * * * * * * * * * field[loop].checked = false;
* * * * * * * * }
* * * * }
</script>

<?php
* * * * echo '<form name="TestSelect_Form" method="GET" action="TestList.php"
enctype="text/plain" style="margin:0px">
* * * * * * * * <input type="radio" name="SelectOrNot" value="Select_All"
onClick="CheckAll(document.TestSelect_Form.SelectM odule)"
style="position:absolute;left:290px;top:140px;z-index:6">
* * * * * * * * <input type="radio" name="SelectOrNot" value="Uncheck_All"
onClick="UnCheckAll(document.TestSelect_Form.Selec tModule)" checked
style="position:absolute;left:400px;top:140px;z-index:7">';
* * * * $file_name = "CONF/testselect.txt";
* * * * $fp_TstSlct = fopen("$file_name", "r") or die("Some error occurred
while opening 'testSelect.txt");
* * * * $TopPos = 170;
* * * * while(!feof($fp_TstSlct))
* * * * {
* * * * * * * * $TstSlct_data = fgets($fp_TstSlct);
* * * * * * * * preg_match_all("/([#]*)(.*)\s(\d+)\s*[;#]*(.*)/", $TstSlct_data,
$match, PREG_PATTERN_ORDER);
* * * * * * * * if ( $match[2][0] != NULL ) { * //Checking whether module is present
or not. May be only comments have been put. For. e.g.
###################DSL HOME#########
* * * * * * * * * * * * if ( $match[1][0] == NULL ) { * //Checking if line is starting with
'#' or not
* * * * * * * * * * * * * * * * echo '<input type="checkbox" name="SelectModule[]" value=' .
$match[2][0] . ' checked="checked" style="position:absolute;left:
90px;top:' . $TopPos . 'px">';
* * * * * * * * * * * * } else {
* * * * * * * * * * * * * * * * echo '<input type="checkbox" name="SelectModule[]" value=' .
$match[2][0] . ' style="position:absolute;left:90px;top:' . $TopPos .
'px">';
* * * * * * * * * * * * }

* * * * * * * * * * * * echo '<div id="text3" style="position:absolute; overflow:hidden;
left:130px; top:' . $TopPos . 'px; width:79px; height:16px"><div
class="wpmd">';
* * * * * * * * * * * * echo '<div><font face="Arial">' . $match[2][0] . '</font></div>';
* * * * * * * * * * * * echo '</div></div>';

* * * * * * * * * * * * $TopPos = $TopPos + 25;
* * * * * * * * }
* * * * }
* * * * fclose($fp_TstSlct);

* * * * echo '<input name="ConfigureTestList" type="submit" value="Next"
style="position:absolute;left:326px;top:403px;z-index:16">
* * * * </form>

* * * * <div id="text1" style="position:absolute; overflow:hidden; left:
265px; top:118px; width:69px; height:18px; z-index:9"><div
class="wpmd">
* * * * <div><font class="ws12" face="Arial">Select All</font></div>
* * * * </div></div>

* * * * <div id="text2" style="position:absolute; overflow:hidden; left:
371px; top:118px; width:85px; height:18px; z-index:10"><div
class="wpmd">
* * * * <div><font class="ws12" face="Arial">Uncheck All</font></div>
* * * * </div></div>';
?>

Please someone suggest the solution as I'm totally stuck.
Thanks a lot in advance.
you can add a unique class name to all checkboxes and then use
getElementsByClassName

example:

<script language="javascript">
var checkAll = {
run:function(className, checked){
var elements = document.getElementsByClassName(className);
for(i in elements){
elements[i].checked = checked;
}
}
}
</script>

<form ...>
<input type="checkbox" name="SelectModule[]" value="some value 1"
class="checkme anyOtherClass1" />
<input type="checkbox" name="SelectModule[]" value="some value 2"
class="checkme anyOtherClass2" />
<input type="checkbox" name="SelectModule[]" value="some value 3"
class="checkme anyOtherClass3" />
<input type="checkbox" name="SelectModule[]" value="some value 4"
class="checkme anyOtherClass4" />
</form>
<a href="javascript:checkAll.run('checkme',true)">che ck</a>
<a href="javascript:checkAll.run('checkme',false)">un check</a>
Oct 29 '08 #12
On Oct 29, 9:36 am, Suhas Dhoke wrote:
<snip>
I've modified your code, and here is the updated version..

#####
<script language="JavaScript" type="text/javascript">
function checkAll (rad, selectType) {
for (var i=0;i < document.TestSelect_Form.elements.length;i++) {
var e = document.TestSelect_Form.elements[i];
if ('checkbox' == e.type && 'select' == selectType) {
Should the - 'select' == selectType - be inside the - for - loop and
repeatedly evaluated, given that it will always evaluate to the save
value?
e.checked = true;
} else {
This - else - branch is going to be entered whenever - 'checkbox' ==
e.type - is false, that does not seem like a good idea as it means
that all radio controls will have their - checked - properties set to
false.
e.checked = false;
}
}
rad.checked = true;}
<snip>^^^^^^^^^^^^^^^^^^^
Presumably this is here partly to mitigate for radio controls having
their - checked - properties set to false in the - else - branch?

Consider:-

function checkAll(rad, selectType){
var newValue = ('select' == selectType);
var el, c, els = rad.form.elements;
if((c = els.length)){
do{
if(
(el = els[--c])&&
(el.type == 'checkbox')
){
el.checked = newValue;
}
}while(c);
}
}

Richard.
Oct 29 '08 #13
On Oct 29, 5:39*am, khaled.jo...@gmail.com wrote:

[snip]
>
you can add a unique class name to all checkboxes and then use
getElementsByClassName
If you are developing exclusively for the very latest browsers, which
would not include the release versions of IE, then you can use that
method. However, it would not be an appropriate solution to this
problem.
>
example:

<script language="javascript">
Don't use the language attribute. Use type="text/javascript".
var checkAll = {
* * run:function(className, checked){
This is very odd.
* * * * var elements = document.getElementsByClassName(className);
Blows up in most browsers.
* * * * for(i in elements){
Filter for-in loops.
* * * * * * elements[i].checked = checked;
* * * * }
* * }}

</script>

<form ...>
<input type="checkbox" name="SelectModule[]" value="some value 1"
class="checkme anyOtherClass1" />
<input type="checkbox" name="SelectModule[]" value="some value 2"
class="checkme anyOtherClass2" />
<input type="checkbox" name="SelectModule[]" value="some value 3"
class="checkme anyOtherClass3" />
<input type="checkbox" name="SelectModule[]" value="some value 4"
class="checkme anyOtherClass4" />
</form>
<a href="javascript:checkAll.run('checkme',true)">che ck</a>
<a href="javascript:checkAll.run('checkme',false)">un check</a>
Never use the javascript pseudo-protocol. Turn off scripting and see
that your document makes no sense.
Oct 30 '08 #14

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

Similar topics

13
by: Adrian Parker | last post by:
I have a PHP generated page which displays X many records. Each record has a checkbox preceding it. The user checks several checkboxes, and hits a delete button. All the corresponding records...
6
by: LRW | last post by:
I have no idea if this is more a PHP question or Javascript question, because my problem hinges equally on both. I have a PHP script that queries a database and creates a list of rows for each...
8
by: John Banta | last post by:
Hi, I have created a 12 month calendar where each day has a check box whereby the user can indicate if that day is available or not available for a certain event. The calendar is 'drawn' in a...
2
by: Edward | last post by:
The following html / javascript code produces a simple form with check boxes. There is also a checkbox that 'checks all' form checkboxes hotmail style: <html> <head> <title></title> </head>...
2
by: jimi_xyz | last post by:
Sorry if this isn't the correct group, i don't think there is a group for straight HTML. I am trying to create a type of search engine. There are two radio buttons at the top, in the middle there...
10
by: rn5a | last post by:
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....
10
by: tadisaus2 | last post by:
Hello, I want to have a user to check at least 2 check boxes and NO more than 2 boxes. I have different checkbox names because I stored each nam on different field. I tried this code but nothing...
2
by: dkultasev | last post by:
Hello, I have small script which generates some listboxes. Their names are listXX (list01, list02, list03....). How to check if there are checked or not ? If I have 1 listbox and have it's name I...
1
by: Anuj | last post by:
Hi, since sometime I'm stuck in a problem where I want to check or uncheck all the checkboxes. If I'm choosing name for the checkbox array as 'chkbx_ary' then I'm able to check/uncheck all the...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.