Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-01-2007, 05:28 PM   PM User | #1
Tankery
New to the CF scene

 
Join Date: Jan 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Tankery is an unknown quantity at this point
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
Tankery is offline   Reply With Quote
Old 01-01-2007, 06:22 PM   PM User | #2
1212jtraceur
Regular Coder

 
Join Date: Oct 2006
Posts: 206
Thanks: 1
Thanked 0 Times in 0 Posts
1212jtraceur is an unknown quantity at this point
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.
1212jtraceur is offline   Reply With Quote
Old 01-01-2007, 07:49 PM   PM User | #3
Tankery
New to the CF scene

 
Join Date: Jan 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Tankery is an unknown quantity at this point
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
Tankery is offline   Reply With Quote
Old 01-01-2007, 08:52 PM   PM User | #4
Ancora
Banned

 
Join Date: Oct 2005
Location: I'm in GMT -5
Posts: 314
Thanks: 0
Thanked 1 Time in 1 Post
Ancora is on a distinguished road
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>
Ancora is offline   Reply With Quote
Old 01-01-2007, 09:22 PM   PM User | #5
Tankery
New to the CF scene

 
Join Date: Jan 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Tankery is an unknown quantity at this point
Thank you!!!!

Thanks for both of your help. It works great now!
Tankery is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:46 AM.


Advertisement
Log in to turn off these ads.