Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass value for unchecked boxes 1

Status
Not open for further replies.

coders4hire

IS-IT--Management
Dec 31, 2003
22
US
trollacious answered another one of my questions today, and I'm trying to modify the code to work with this problem. For ANY unchecked boxes in my form, I want to pass a value of '0'. Can I do this without identifying every box?

Is this efficient below?

Code:
function VerifyForm(subscribeForm){
var myblank = '';
				
//list of checkboxes to evaluate
var cbox = ['a1','a2','a3'];
				
for (var i=0;i<cbox.length;i++) {
   var onecbox = document.getElementById(cbox[i])
   var checked = onecbox.checked;
   if (checked)
	 {
	 myblank == cbox[i].value;
	 }
	 else
	 {
	 myblank == '0';
	 }
	}
	 document.getElementById(this).value = myblank;
    }

Doug
 
Don't use the double equals signs to assign a value to a variable. The double equals sign just compares the items on either side and returns true or false, which in your code would do nothing.

As well, this is a reserved word that refers to the object the code is in at the time, in this case the function itself, which is not what you want.

Try this instead:
Code:
for (var ci=0;ci<cbox.length;ci++)
  {
  var onecbox = document.getElementById(cbox[ci]);
  var checked = onecbox.checked;
  if (checked)
    {
    myblank = onecbox.value;
    }
  else
    {
    myblank = '0';
    }
  }
document.getElementById('yourelementIDhere').value = myblank;
}

Lee
 
Just looking through your code again. Did you want to concatentate myblank to hold ALL the values from the checkboxes? The code, as it is now, only retains the value in the last checkbox before writing it to the final element referred to.

Lee
 
I actually changed my mind ;)

I would like to ONLY change the values of the unchecked boxes that are in my array to equal '0'. I tried modifying your code a little but it's not quite there:

Code:
function VerifyForm(subscribeForm){
var cbox = ['accepts_newsletter','accepts_events','accepts_promotions','accepts_product','accepts_technical'];

for (var ci=0;ci<cbox.length;ci++) {
   var onecbox = document.getElementById(cbox[ci]);
   var checked = onecbox.checked;
   if (!checked) {
   onecbox.value = '0';
   }
  }
 }

However, it's still not passing '0'.

Doug
 
If the boxes are unchecked, then they have NO value. If you want to pass a value of zero for the form element when the page is submitted, I'd suggest changing the checkboxes to sets of 2 radio buttons, with the first one checked by default with the value of zero. That would eliminate the need for the JS function that you've presented for this question.

Code:
<input type="radio" name="choice1" id="choice1a" value="0" checked /> Default Choice<br />
<input type="radio" name="choice1" id="choice1b" value="2" /> Deliberate Choice

Lee
 
A form won't pass a value for a checkbox that isn't checked, it won't be defined at all. What you'll need to do on the page that processes the form is check for the checkbox variables, and if they're not defined, assign a value of zero to that variable.

You could use a hidden variable, and if the checkbox is checked, it'll be the selected value, and if it isn't checked, it'll be 0.

Code:
<script type="text/javascript">
function checkcbox(onecbox, onevalue)
{
var hiddenname = 'a' + onecbox.name.substr(1);
var hiddenelement = document.getElementById(hiddenname);
if (onecbox.checked)
  {
  hiddenelement.value = onecbox.value;
  }
else
  {
  hiddenelement.value = '0';
  }
}
</script>
<input type="checkbox" name="e1" onclick="checkcbox(this, "10")"> Check me!
<input type="hidden" name="a1" id="a1" value="0">

Lee
 
You can switch the values using the new function I gave as an example. As long as the hidden input has a different name than the checkbox, you won't get a comma-delimited value on the processing page. Name the hidden input what you want to pass the value, and name the checkbox anything you want that you can use to get the hidden input name from.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top