...

View Full Version : Javascript Checkbox array help needed



Tankery
01-01-2007, 05:28 PM
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:)

1212jtraceur
01-01-2007, 06:22 PM
Try this:



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

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.

Tankery
01-01-2007, 07:49 PM
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

Ancora
01-01-2007, 08:52 PM
Tankery:

Validate a form using onsubmit:



<!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>

Tankery
01-01-2007, 09:22 PM
Thanks for both of your help. It works great now!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum