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

P

PhpCool

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.SelectModule)"
style="position:absolute;left:290px;top:140px;z-index:6">
<input type="radio" name="SelectOrNot" value="Uncheck_All"
onClick="UnCheckAll(document.TestSelect_Form.SelectModule)" 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.
 
S

Stevo

PhpCool said:
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.
 
M

Matt Kruse

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
 
T

Thomas 'PointedEars' Lahn

Conrad said:
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]][][][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:

js> var привет = "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
 
D

David Mark

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.
 
T

Thomas 'PointedEars' Lahn

Thomas said:
Conrad said:
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
 
S

Suhas Dhoke

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.SelectModule)"
style="position:absolute;left:290px;top:140px;z-index:6">
                <input type="radio" name="SelectOrNot" value="Uncheck_All"
onClick="UnCheckAll(document.TestSelect_Form.SelectModule)" 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;
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.
 
K

khaled.jouda

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.SelectModule)"
style="position:absolute;left:290px;top:140px;z-index:6">
                <input type="radio" name="SelectOrNot" value="Uncheck_All"
onClick="UnCheckAll(document.TestSelect_Form.SelectModule)" 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.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)">check</a>
<a href="javascript:checkAll.run('checkme',false)">uncheck</a>
 
R

Richard Cornford

On Oct 29, 9:36 am, Suhas Dhoke wrote:
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;
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.
 
D

David Mark

On Oct 29, 5:39 am, (e-mail address removed) 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.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)">check</a>
<a href="javascript:checkAll.run('checkme',false)">uncheck</a>


Never use the javascript pseudo-protocol. Turn off scripting and see
that your document makes no sense.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top