View Full Version : Issue with Multiple Checkboxes and Turning them off

07-20-2009, 02:14 PM
Hello...I assume this should be relatively simple but can't seem to figure it out and have searched all over the net.

I have an .asp page that contains a form with multiple checkboxes for users to register for classes. When the classes fill up I want to be able to not allow them to register.

Right now I am doing the following:

function regclosed()
alert("Registration for this class has closed." + '\n' + '\n' + "Please select another class.");

Then for each checkbox I am using onclick = "regclosed()"

The problem that I am having is that even though I only put the 'onclick' for certain checkboxes...as soon as I click a checkbox that has the regclosed() it clears every box that was checked - not just the one that has onclick="regclosed()"

Philip M
07-20-2009, 02:35 PM
Try this:-

Course A <input type = "checkbox" name = "chk1" id = "chk1" onclick = "regclosed(this)"><br>
Course B <input type = "checkbox" name = "chk2" id = "chk2" onclick = "regclosed(this)"><br>
Course C <input type = "checkbox" name = "chk3" id = "chk3" onclick = "regclosed(this)"><br>

<script type = "text/javascript">

function regclosed(which) {
alert("Registration for this class has closed." + '\n' + '\n' + "Please select another class.");
which.checked = false;


“The old believe everything; the middle aged suspect everything; the young know everything.” - Oscar Wilde (Irish Poet, Novelist, Dramatist and Critic, 1854-1900)

07-20-2009, 02:37 PM
I thought of doing this but I was trying to avoid having to spell out each course in the script. There is over 50 of them and they change frequently.

07-20-2009, 02:51 PM
well wait a min...I think I mis-read your code...

07-20-2009, 03:01 PM
that did it...thanks. I mis-read the 'this' and 'which'

12 Pack Mack
07-20-2009, 03:12 PM

Another approach. Disable the checkbox for closed classes. No changes to the mark-up are needed.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">

var registrationClosed = ["Accounting 287", "Business Law 344", "Biology 215",
"Psychology 448", "Marketing 100"];

function init(){

var nForm = document.forms[0];
var nCourses = nForm.getElementsByTagName('input');
for (i=0; i<registrationClosed.length; i++)
for (n=0; n<nCourses.length; n++)
if (nCourses[n].type == "checkbox")
var courseTitle = nCourses[n].parentNode.firstChild.data;
courseTitle = courseTitle.replace(/\s+$/,"").replace(/^\s+/,"");
if (courseTitle == registrationClosed[i])
nCourses[n].disabled = true;


navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);

<style type="text/css">

body {margin-top: 60px;}
label {font-family: tahoma; font-size: 11pt; display: block;}

<form action="" method="post">

<label> Intermediate Accounting 324 <input type="checkbox" name="course_345775"></label>
<label> Accounting 287<input type="checkbox" name="course_345792"></label>
<label> Sociology 254 <input type="checkbox" name="course_000252"></label>
<label>Business Law 344 <input type="checkbox" name="course_785775"></label>
<label>Biology 215 <input type="checkbox" name="course_002375"></label>
<label> Creative Writing 109 <input type="checkbox" name="course_349075"></label>
<label>Psychology 448<input type="checkbox" name="course_388572"></label>
<label>American History 142 <input type="checkbox" name="course_300096"></label>
<label> Marketing 100 <input type="checkbox" name="course_858520"></label>


07-20-2009, 03:17 PM
12 Pack Mack - Interesting this looks like it may be a simpler way to 'turn off' the registration from a maintenance standpoint.

I will give it a try.

FYI - I am a self taught javascript (well web developer in general) - hence the reason for the easy (i think) questions.

07-20-2009, 03:29 PM
12 Pack Mack - is there a trick to get your code to work with Firefox? Your code is a bit more advanced than my skill level, I wonder if it has something to do with:
navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);

What would really be cool is if I could set a time and date for the registration to close - which would be different for each class - but I haven't even begun to research that yet. Just trying to get this to work correctly first.

12 Pack Mack
07-20-2009, 03:46 PM

I tested it in FF, and noticed that the last item in the array of closed classes didn't disable. I corrected the code in my previous post.

Do not change the "navigator.appName" line. That is required to make the init() function execute in IE and other browsers.

Do you have the checkboxes surrounded by <label> tags in your mark-up? If not, it's invalid mark-up.

You really should be doing all of this server-side.

Your ASP shouldn't create the mark-up for closed classes, based upon info in your database.

Just get the code working with your mark-up, first, then maybe I'll think about including an "expiration" date.

07-20-2009, 03:55 PM
this is a typical check box on my page...

<input type="checkbox" id="pac" name="pac" value="pac_Feb_4">

Is this incorrect? Of course it is within form tags.

12 Pack Mack
07-20-2009, 04:25 PM

Did I write the word "form"?

No. I wrote, "<label>"

I posted a complete .html document. Copy and save it as an .html document. What else, for Heaven's sake?

Look at the mark-up I posted. See, "<label>" tags.

07-20-2009, 04:30 PM
thanks for your time.