473,327 Members | 2,094 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,327 software developers and data experts.

Check/Uncheck all checkboxes with name as '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
1 5156
SAM
Le 10/28/08 5:53 PM, Anuj a écrit :
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[]'

document.forms[0].elements['chkbx_ary[]'];

document.forms['TestSelect_Form'].elements['chkbx_ary[]'];

if several checkboxes of same name
that will return [object NodeList]

ordinary checboxes don't wear same name ...
how do you recognize one from the other ?

some tests :

var chc = document.forms['TestSelect_Form']['chkbx_ary[]'];
alert( chc[0].value);
alert( document.forms['TestSelect_Form']['chkbx_ary[]'][0].value );
for(var i=0, n=chc.length; i<n; i++)
alert(chc[i].value)
var t = '';
for(var i=0, n=chc.length; i<n; i++)
t += chc[i].name + '\'s value = ' + chc[i].value +'\n';
alert(t);

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.
onclick="CheckAll(document.TestSelect_Form['SelectModule[]'])
or (because it is in the form) :
onclick="CheckAll(this.form['SelectModule[]']);
<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
please give the result in html !

where is the problem ? which line(s) ?
Please someone suggest the solution as I'm totally stuck.
Thanks a lot in advance.
You can't use :
document.TestSelect_Form.chkbx_ary[]
because brackets are of JS language
so you'll do :
document.TestSelect_Form.elements['chkbx_ary[]']
or :
document.getElementsByName('chkbx_ary[]');
to get the collection of checkboxes named 'chkbx_ary[]'
--
sm
Oct 28 '08 #2

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

Similar topics

7
by: Mike Lopez | last post by:
Hello. I need to set a checkbox to the "checked" state and prevent the user from unchecking it. I tried using "disabled", but then the value is not passed on the Post to an ASP page. Anyone...
8
by: pw | last post by:
Hi, I need to create a function in javascript to check or uncheck all checkboxes in a form. From what I understand, I can do this either by specifying the name of the check box fields such as: ...
2
by: Chris Davoli | last post by:
How do you enable a check box in the GridView. I selected Checkbox Field in the Columns of the GridView, and the check box shows up in the Grid view, but it is disabled. How do I enable it so I can...
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....
4
by: chengsi | last post by:
Hi, I have a "continuous" subform which is linked to a table which has a checkbox field. I would like to create a Check All/Uncheck All checkbox control that both checks and disables the...
12
by: swebster | last post by:
I need to zero in on a specific section of my jsp form during a hide to uncheck the checkboxes in that section. Currently, anything I try either unchecks all checkboxes on the form or doesn't work...
3
by: Ryanfromscotland | last post by:
Hey there everyone, this is my first post so be nice :-) I have wrote quite a bit so the actual question is written out at the end if you want to skip on and see if you can answer it without the rest...
3
by: BibhuAshish | last post by:
Hi, I want to check or uncheck a number of checkboxes by clicking an image. My html code is given below: <TABLE> <TR> <TD>
13
by: PhpCool | 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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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: Shællîpôpï 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.