Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Issue with Multiple Checkboxes and Turning them off

    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:

    Code:
    function regclosed()
    {
    alert("Registration for this class has closed." + '\n' + '\n' + "Please select another class.");
    this.checked=false;
    }
    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()"

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Try this:-

    Code:
    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;
    }
    
    </script>

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

  • #3
    New Coder
    Join Date
    Jul 2009
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New Coder
    Join Date
    Jul 2009
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well wait a min...I think I mis-read your code...

  • #5
    New Coder
    Join Date
    Jul 2009
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    that did it...thanks. I mis-read the 'this' and 'which'

  • #6
    Banned
    Join Date
    Mar 2009
    Posts
    248
    Thanks
    3
    Thanked 68 Times in 66 Posts
    jbsmith:

    Another approach. Disable the checkbox for closed classes. No changes to the mark-up are needed.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Form</title>
    <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);	
    
    </script>
    <style type="text/css">
    
    	 body {margin-top: 60px;}
    	 label	{font-family: tahoma; font-size: 11pt; display: block;}	
    
    </style>
    </head>
    	<body>
    		<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>
    		
    		</form>
    	</body>
    </html>
    Last edited by 12 Pack Mack; 07-20-2009 at 02:40 PM.

  • #7
    New Coder
    Join Date
    Jul 2009
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #8
    New Coder
    Join Date
    Jul 2009
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    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.

  • #9
    Banned
    Join Date
    Mar 2009
    Posts
    248
    Thanks
    3
    Thanked 68 Times in 66 Posts
    jbsmith:

    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.

  • #10
    New Coder
    Join Date
    Jul 2009
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this is a typical check box on my page...

    Code:
    <input type="checkbox" id="pac" name="pac" value="pac_Feb_4">
    Is this incorrect? Of course it is within form tags.

  • #11
    Banned
    Join Date
    Mar 2009
    Posts
    248
    Thanks
    3
    Thanked 68 Times in 66 Posts
    jbsmith:

    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.

  • #12
    New Coder
    Join Date
    Jul 2009
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for your time.
    Last edited by jbsmith; 07-20-2009 at 07:39 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •