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

2 questions regarding Dynamically created checkboxes

P: n/a
Hi guys,

On my page I dynamically create different checkboxes.

<input type="checkbox" name="ch1" value="some value"> option 1
<input type="checkbox" name="ch2" value="some value"> option 2
<input type="checkbox" name="ch3" value="some value"> option 3
<input type="checkbox" name="ch4" value="some value"> option 4

The reason they have different names is because of a different script which
allows me to shift-select a range of checkboxes. (there are 200+ on a page).

My Questions:

1. How can I have an alertbox popup when NONE of the checkboxes are checked
and the user submits the form?
2. How could I have a "select all" option on these checkboxes?

Any help would be so much appreciated!

Ond.
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Ondernemer wrote:
1. How can I have an alertbox popup when NONE of the checkboxes are checked
and the user submits the form?
Put this in the javascript-section:
function validate(frm) {
var valid = false;
for (i=0;i<frm.length;i++) {
if (frm.elements[i].type == "checkbox" && frm.elements[i].checked) {
valid = true;
break;
}
}
if (!valid) {alert("Please select at least one checkbox");}
return valid;
}

When you submit the form you can call this function and it will
determine whether at least one checkbox is checked:
<form action="page.php" onsubmit="return validate(this);">
2. How could I have a "select all" option on these checkboxes?

Try this function:
function selectAll(frm,checkedStatus) {
for (i=0;i<frm.length;i++) {
if (frm.elements[i].type == "checkbox") {
frm.elements[i].checked = checkedStatus;
}
}
}

It runs through the form, finds the checkboxes and sets the
checkedstatus to whatever you want. You can call them from the form like
this:

<form action="page.php" onsubmit="return validate(this);">
<input type="button" value="Select All"
onclick="selectAll(this.form,true);">
<input type="button" value="DeSelect All"
onclick="selectAll(this.form,false);">
Jul 23 '05 #2

P: n/a
You simply......... rock!
Jul 23 '05 #3

P: n/a
Ondernemer wrote:
You simply......... rock!


Well, thank you very much.

However, if you have the exact number of checkboxes and they have the
same type of name (i.e. "ch1","ch2" and so on) you could use this to
avoid running through the whole form:
<script type="text/javascript">
function validate(frm,num) {
var valid = false;
for (i=1;i<num;i++) {
if (frm.elements["ch"+i].checked) {
valid = true;
break;
}
}
if (!valid) {alert("Please select at least one checkbox");}
return valid;
}

function selectAll(frm,num,checkedStatus) {
for (i=1;i<=num;i++) {
frm.elements["ch"+i].checked = checkedStatus;
}
}
</script>

and start them just as before but with one more argument:
<form onsubmit="return validate(this,5);">
<input type="button" value="Select All"
onclick="selectAll(this.form,5,true);">
<input type="button" value="DeSelect All"
onclick="selectAll(this.form,5,false);">

5 is the number of checkboxes - you probably should change that ;-)
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.