473,480 Members | 2,230 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Selecting/deselecting checkboxes

Jez
Hi,

I have created the following functions to select and deselect
checkboxes in a form ...

function check(checkbox) {
for (i = 0; i < checkbox.length; i++) {
checkbox[i].checked = true;
}
return "Select all";
}

function uncheck(checkbox) {
for (i = 0; i < checkbox.length; i++) {
checkbox[i].checked = false;
}
return "Select none";
}

The form itself looks like this ...

<form name="form" action="delete.php" method="post">
<input type="checkbox" name="id" value="1" />Option 1<br />
<input type="checkbox" name="id" value="2" />Option 2<br />
<input type="checkbox" name="id" value="3" />Option 3<br />
<input type="button" value="Select all"
onClick="this.value=check(this.form.id)" />
<input type="button" value="Select none"
onClick="this.value=uncheck(this.form.id)" />
<input type="submit" name="submit" value="Delete selected" />
</form>

This all seems to work very well (although constructive critisism
would be welcome), however I actually need to name the input 'id[]'
because I'm generating an array of values. Unfortunately the funtions
don't seem to like '[]'. Is there anything I can do?

Thank you in advance for your help. It's greatly appreciated!

Jez
Jul 20 '05 #1
5 22928
Feeding an array of checkboxes into a php script?

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

or try:

<input type="button" value="Select all"
onClick="this.value=check(this.form.elements['id[]'])" />
<input type="button" value="Select none"
onClick="this.value=uncheck(this.form.elements['id[]'])" />

"Jez" <je**********@btinternet.com> wrote in message
news:ad**************************@posting.google.c om...
Hi,

I have created the following functions to select and deselect
checkboxes in a form ...

function check(checkbox) {
for (i = 0; i < checkbox.length; i++) {
checkbox[i].checked = true;
}
return "Select all";
}

function uncheck(checkbox) {
for (i = 0; i < checkbox.length; i++) {
checkbox[i].checked = false;
}
return "Select none";
}

The form itself looks like this ...

<form name="form" action="delete.php" method="post">
<input type="checkbox" name="id" value="1" />Option 1<br />
<input type="checkbox" name="id" value="2" />Option 2<br />
<input type="checkbox" name="id" value="3" />Option 3<br />
<input type="button" value="Select all"
onClick="this.value=check(this.form.id)" />
<input type="button" value="Select none"
onClick="this.value=uncheck(this.form.id)" />
<input type="submit" name="submit" value="Delete selected" />
</form>

This all seems to work very well (although constructive critisism
would be welcome), however I actually need to name the input 'id[]'
because I'm generating an array of values. Unfortunately the funtions
don't seem to like '[]'. Is there anything I can do?

Thank you in advance for your help. It's greatly appreciated!

Jez

Jul 20 '05 #2
Thanks, that's excellent!

My function now works perfectly unless there's only one checkbox, in
which case it doesn't work at all.

Any ideas?

Jez

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3
A single checkbox doesn't have a length property, so you can't loop through
using id.length, you have to specifically check the id

An example I pulled from a page which dynamically generates a group of
checkboxes:

// check for single checkbox by seeing if an array has been created
var cblength=document.forms['inbox'].elements['del'].length;
if(typeof cblength=="undefined")
{
if(document.forms['inbox'].elements['del'].checked==true) count++;
}
else
{
for(i=0;i<document.forms['inbox'].elements['del'].length;i++)
{
if(document.forms['inbox'].elements['del'][i].checked) count++;
}
}
Assuming you are using php

<input type="checkbox" name="bob" value="3">

and in the php

<?php
if(isset(_POST["bob"])
{
// checkbox 'bob' has been checked in the form
$bob=$_POST["bob"];
// variable $bob will be set to the value of checkbox 'bob', in this case
"3"
}
else
{
// checkbox 'bob' has not been checked, set a default value
$bob="0";
}

"Jez Hailwood" <je**********@btinternet.com> wrote in message
news:3f*********************@news.frii.net...
Thanks, that's excellent!

My function now works perfectly unless there's only one checkbox, in
which case it doesn't work at all.

Any ideas?

Jez

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Jul 20 '05 #4
Jez
Problem now solved with a much simpler solution ...

function selectAll(state) {
for (i = 0; i < document.form.elements.length; i++) {
var checkbox = document.form.elements[i];
checkbox.checked = state;
}
}

<input type="button" value="Select all" onClick="selectAll(true);" />
<input type="button" value="Clear all" onClick="selectAll(false);" />

Jez
Jez Hailwood <je**********@btinternet.com> wrote in message news:<3f*********************@news.frii.net>...
Thanks, that's excellent!

My function now works perfectly unless there's only one checkbox, in
which case it doesn't work at all.

Any ideas?

Jez

Jul 20 '05 #5
elsint
1 New Member
Great solution!!

Problem now solved with a much simpler solution ...

function selectAll(state) {
for (i = 0; i < document.form.elements.length; i++) {
var checkbox = document.form.elements[i];
checkbox.checked = state;
}
}

<input type="button" value="Select all" onClick="selectAll(true);" />
<input type="button" value="Clear all" onClick="selectAll(false);" />

Jez


Jez Hailwood <jez.hailwood@btinternet.com> wrote in message news:<3f171020$0$203$75868355@news.frii.net>...[color=blue]
> Thanks, that's excellent!
>
> My function now works perfectly unless there's only one checkbox, in
> which case it doesn't work at all.
>
> Any ideas?
>
> Jez[/color]
Jul 14 '06 #6

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

Similar topics

9
4634
by: Phil Powell | last post by:
I am producing a form using PHP on the back end and Javascript on the front end. The resulting script will come to the browser as follows: <script> <!-- function selectAll() { moveElement =...
7
1864
by: Rodney King | last post by:
Hi, I have developed an ASP page which dynamically displays a list of checkbox options based on a SQL statement. Here is my code: <div style="OVERFLOW:auto; Height: 150px"> <table> <% dim...
1
17363
by: Eric | last post by:
Hi all, I have a form that generates a dynamic number of rows from a value passed in via querystring. I have a one static row in my form with a "master" checkbox that I have deemed "Select...
1
3317
by: Bob Loveshade | last post by:
I am looking for an example that shows how to select and highlight multiple rows in a DataGrid. My DataGrid is part of a Web User Control which is contained in an ASPX page. I haven't been...
3
4424
by: newjazzharmony | last post by:
Hello group, I want to automatically select a specific checkbox when a user clicks (selects) a specific item in a radiobutton group. Both controls are in the same form. Let's say for...
0
4345
by: SirMikesALot | last post by:
I'm pretty good at Excel, but my skills in Visual Basic are very limited. I found script on-line that creates & opens a user form, allows you to select your worksheets, prints the selected...
0
1506
by: TarekSaad | last post by:
I am using VB2005 and have created a dynamic list of checkboxes and depending on which file the user selects there may be between 1 and 20 checkboxed that appear in a form. The user then checks...
0
10979
by: jeremy | last post by:
Had a tough time figuring this one out and couldn't find a good solution, so I thought I would post this and hopefully it will help someone out. When using DataBind to dynamically bind a list to...
5
7397
by: purush2purush | last post by:
HI All.... cud any one provide me the code for : Selecting mulitiple checkboxes and sending mail when clicked on submit button. The checkboxes are retrived from the Mysql...
0
7049
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,...
0
7092
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...
0
5348
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,...
1
4790
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...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
565
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
188
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...

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.