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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript Checkbox array help needed

    I am trying to get this code to pop up a confirm box if either one or many checkboxes in the array are checked. But I only want it to pop up once regardless if there is only one checkbox or many checked.

    It is doing two things wrong:

    When there is only one checkbox it does not pop up at all.
    When there are many checkboxes it is popping up a confirm box for each checkbox.

    function checkform(){
    <!--
    for (i=0; i<document.Form1.CancelClass.length; i++)

    if (document.Form1.CancelClass[i].checked)

    {
    var answer = confirm ("Are you sure you want to cancel a class?")

    if (answer)
    {

    document.Form1.submit();
    }
    else
    return false;
    }
    }


    I would appreciate any help. Thanks

  • #2
    Regular Coder
    Join Date
    Oct 2006
    Posts
    206
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Try this:

    PHP Code:
    function checkform(){
    var 
    done false;
    for (
    i=0i<document.Form1.CancelClass.length && !donei++)

    if (
    document.Form1.CancelClass[i].checked)

    {
    done true;
    var 
    answer confirm ("Are you sure you want to cancel a class?")

    if (
    answer)
    {

    document.Form1.submit();
    }
    else
    return 
    false;
    }

    I added a boolean variable 'done', which controls whether the loop iterates again. Of course, to stop iteration, 'done' must equal true; I set it to true if one of the boxes are checked.

  • #3
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you! But when there is only one checkbox it still doesn't trigger the confirm. Is this because it is no longer an array? The done works great when there is more than one checkbox. I only get one prompt.

    Do you know how I would add it for just one checkbox dumped on the form?

    thanks

  • #4
    Banned
    Join Date
    Oct 2005
    Location
    I'm in GMT -5
    Posts
    314
    Thanks
    0
    Thanked 1 Time in 1 Post
    Tankery:

    Validate a form using onsubmit:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Any Title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    
    	function validate(){
    
    		var nBox = document.getElementsByName('CancelClass');
    		var count = 0;
    		for (i=0; i<nBox.length; i++)
    			{
    			 if (nBox[i].checked)
    				{
    				 count++;
    				}
    			}
    		if (count > 0)
    			{
    			 if (confirm("Are you sure you want to cancel a class?"))
    			 	{
    				 return true;
    				}
    			 else 	{return false}
    			}
    		return true;
    	}
    	
    </script>
    <style type="text/css">
    
    	 body {background-color:#eae3c6;margin-top:60px}
    	 form {width:300px;margin:auto}
    	 fieldset {width:280px;background-color:#f0fff0;border:1px solid #87ceeb}
    	 legend {font-family:georgia;font-size:14pt;color:#00008b;background-color:#87ceeb;padding-left:3px;padding-right:3px;margin-bottom:5px}
    	 label {font-family:times;font-size:12pt;color:#00008b;padding:5px;display:block}
    	.submitBtn {font-family:tahoma;font-size:10pt;display:block;margin-left:auto;margin-right:auto;margin-top:5px;margin-bottom:5px}
    	
    </style>
    </head>
    	<body>
    		<form method="post" action="anything.php" onsubmit="return validate()">
    		   <fieldset>
    			<legend>Classes</legend>
    				<label>A: <input type='checkbox' name='CancelClass'></label>
    				<label>B: <input type='checkbox' name='CancelClass'></label>
    				<label>C: <input type='checkbox' name='CancelClass'></label>
    				<label>D: <input type='checkbox' name='CancelClass'></label>
    			<input type='submit' name='submit' value="Submit" class='submitBtn'>
    		   </fieldset>
    		</form>
    	</body>
    </html>

  • #5
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thank you!!!!

    Thanks for both of your help. It works great now!


  •  

    Posting Permissions

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