By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,089 Members | 2,191 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,089 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
13 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.