Results 1 to 6 of 6

Thread: Clearing & Counting Checkboxes

  1. #1
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Clearing & Counting Checkboxes

    I'm having a hard time figuring this out, and was wondering if any of you can help me.

    I'm writing a bunch of PHP pages, and some of them I am putting checkboxes on. I figure javascript would be the right language to do this in.

    I want to have a bunch of checkboxes, and to have each checkbox record itself, and like... add 1 to an equation.

    I would also like it so I could have a box which you can click to clear all the checkboxes.

    I hope this picture can help explain what I'm thinking about.

    http://i51.tinypic.com/2czex49.png

    Any help, or links, would be greatly appreciated.

    Thank you guys a ton for helping.
    Last edited by cascadeorca; 02-08-2011 at 08:32 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Play around with this:
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    	<script type="text/javascript">
    	var countboxes = function(formID, checksName, updateID){
    		var maxChecks = new Array();
    		maxChecks['max'] = new Array();
    		maxChecks['checked'] = new Array();
    		var inputs = document.getElementById(formID).getElementsByTagName('input');
    		for(var i = 0; i < inputs.length; i++){
    			if(inputs[i].type == "checkbox" && inputs[i].name == checksName){
    				maxChecks['max'][maxChecks['max'].length] = inputs[i];
    				if(inputs[i].checked){
    					maxChecks['checked'][maxChecks['checked'].length] = inputs[i];
    				}
    			}
    		}
    		document.getElementById(updateID).innerHTML = maxChecks['checked'].length + " / " + maxChecks['max'].length;
    	};
    	var clearChecks = function(formID, checksName){
    		var inputs = document.getElementById(formID).getElementsByTagName('input');
    		for(var i = 0; i < inputs.length; i++){
    			if(inputs[i].type == "checkbox" && inputs[i].name == checksName){
    				inputs[i].checked = false;
    				inputs[i].onchange();
    			}
    		}
    	}
    	window.onload = function(){
    		var formID = "checkboxes", 
    			checksName = "item",
    			updateID = "update";
    			
    		var inputs = document.getElementById(formID).getElementsByTagName('input');
    		for(var i = 0; i < inputs.length; i++){
    			if(inputs[i].type == "checkbox" && inputs[i].name == checksName){
    				inputs[i].onchange = function(){
    					countboxes(formID, checksName, updateID);
    				}
    			}
    		}
    		countboxes(formID, checksName, updateID);
    	};
    	</script>
    </head>
    <body>
    	<span id="update">initializing</span>
    	<form id="checkboxes" name="checkboxes">
    		<input type="checkbox" name="item"/>
    		<input type="checkbox" name="item"/>
    		<input type="checkbox" name="item"/>
    		<input type="checkbox" name="item"/>
    		<input type="checkbox" name="item"/>
    		<input type="button" name="reset" value="Reset Checkboxes" onclick="clearChecks('checkboxes', 'item');"/>
    	</form>
    </body>
    </html>
    Jeremy | jfein.net

  3. The Following User Says Thank You to Nile For This Useful Post:

    cascadeorca (02-07-2011)

  4. #3
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thank you, I absolutely will.

    Edit: the reset checkboxes button works perfectly, however the check boxes don't add correctly. Thank you so much for giving me somewhere to start!
    Last edited by cascadeorca; 02-07-2011 at 02:47 AM.

  5. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    What do you mean they dont add completely? :/ It works fine for me.
    Jeremy | jfein.net

  6. #5
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Oh I see, you have to click somewhere else on the page, and then it adds correctly, however just clicking the box doesn't always add immediately. I have only tested it in IE though. Either way I am still very very greatful.

    works flawlessly in chrome though, lol.

    Browsers are odd things. You are completely awesome for helping me out, I'm so happy with this!
    Last edited by cascadeorca; 02-07-2011 at 06:40 AM.

  7. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    No problem, I'm glad to help

    Here on DD, we like to keep things organized. In an effort to do so, you have the option to set a thread to resolved when an issue is fixed. To make the status of the thread resolved:
    1. Go to your first post
    2. Edit your first post
    3. Click "Go Advanced"
    4. In the dropdown next to the title, select "RESOLVED"
    Jeremy | jfein.net

Bookmarks

Posting Permissions

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