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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stuck with an error - cannot see the problem - Help! 1

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
US
I have a JS function:
Code:
	function clicked(n)
	{
		isBD=document.forms[0].elements["isBD[]"];
		isHZ=document.forms[0].elements["isHZ[]"];
		isOV=document.forms[0].elements["isOV[]"];
		isFUM=document.forms[0].elements["isFUM[]"];
		pcs=document.forms[0].elements["pcs[]"];
		wgt=document.forms[0].elements["wgt[]"];
		vwgt=document.forms[0].elements["vwgt[]"];
		povalue=document.forms[0].elements["povalue[]"];
		toship=document.forms[0].elements["ship[]"];
		totalPCS=document.getElementById('totalPCSID').value;
		totalKGS=document.getElementById('totalKGSID').value;
		totalVKGS=document.getElementById('totalVKGSID').value;
		totalVAL=document.getElementById('totalVALID').value;
		bdPCS=document.getElementById('bdPCSID').value;
		ovPCS=document.getElementById('ovPCSID').value;
		fumPCS=document.getElementById('hzPCSID').value;
		hzPCS=document.getElementById('fumPCSID').value;
		if (toship[n].checked == true) {
			piezas=pcs[n].value;
			if (piezas == 'COPACK') piezas=0;
			totalPCS=(1*totalPCS)+(1*piezas);
			totalKGS=(1*totalKGS)+(1*wgt[n].value);
			totalVKGS=(1*totalVKGS)+(1*vwgt[n].value);
			totalVAL=(1*totalVAL)+(1*povalue[n].value);
			if (isBD[n].value == 'Y' || isBD[n].value == 'y') bdPCS=(1*bdPCS)+(1*piezas);
			if (isHZ[n].value == 'Y' || isHZ[n].value == 'y') hzPCS=(1*hzPCS)+(1*piezas);
			if (isOV[n].value == 'Y' || isOV[n].value == 'y') ovPCS=(1*ovPCS)+(1*piezas);
			if (isFUM[n].value == 'Y' || isFUM[n].value == 'y') fumPCS=(1*fumPCS)+(1*piezas);			
		} else {
			piezas=pcs[n].value;
			if (piezas == 'COPACK') piezas=0;
			totalPCS=(1*totalPCS)-(1*piezas);
			totalKGS=(1*totalKGS)-(1*wgt[n].value);
			totalVKGS=(1*totalVKGS)-(1*vwgt[n].value);
			totalVAL=(1*totalVAL)-(1*povalue[n].value);
			if (isBD[n].value == 'Y' || isBD[n].value == 'y') bdPCS=(1*bdPCS)-(1*piezas);
			if (isHZ[n].value == 'Y' || isHZ[n].value == 'y') hzPCS=(1*hzPCS)-(1*piezas);
			if (isOV[n].value == 'Y' || isOV[n].value == 'y') ovPCS=(1*ovPCS)-(1*piezas);
			if (isFUM[n].value == 'Y' || isFUM[n].value == 'y') fumPCS=(1*fumPCS)-(1*piezas);					
		}
		document.getElementById('totalPCSID').value=totalPCS;
		document.getElementById('totalKGSID').value=formatAsMoney(totalKGS);
		document.getElementById('totalVKGSID').value=formatAsMoney(totalVKGS);
		document.getElementById('totalVALID').value=formatAsMoney(totalVAL);
		document.getElementById('bdPCSID').value=bdPCS;
		document.getElementById('ovPCSID').value=ovPCS;
		document.getElementById('hzPCSID').value=hzPCS;
		document.getElementById('fumPCSID').value=fumPCS;
		xajax_clicked(totalPCS,totalKGS,totalVKGS,totalVAL,bdPCS,ovPCS,hzPCS,fumPCS);
	}

The HTML looks like this:
Code:
<input type="checkbox" name="ship[]" id="ship0ID" value="0"  onclick="clicked('0')" />

As you may have noticed, I am using field names such as ship[], isBD[], etc. I am getting a JS error saying that toship[] is not defined. Notice that I am defining it within the JS snippet.

The code works if I have two or more input lines but it breaks on a single line. I have spent all day looking at this and no matter what, I cannot see what's wrong with it.

Perhaps, I am so convinced that it should work that I am blocking an obvious problem.

Thank you all for your help!



--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 

If there is only one checkbox named 'ship[]' then that's your problem - you have to have more than one checkbox with the same name before they act as an array.

Hope this helps,
Dan

 
You could get around this in your code by detecting only 1 checkbox... perhaps a handy routine which always returned an array even if only one was present would work for you? Something like:

Code:
function getCheckboxArray(theForm, checkboxName) {
   var cbs = theForm.elements[checkboxName];
   if (typeof(cbs.length) == 'undefined') {
      return ([cbs]);
   } else {
      return (cbs);
   }
}

Then replace this:
Code:
toship=document.forms[0].elements["ship[]"];

with this:

Code:
toship = getCheckboxArray(document.forms[0], 'ship[]');

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
@BillyRayPreacherSon,

This really solved the problem and opened my eyes to a new level of JS ... not to mention saved my life!

Thank you so very much !!!!!




--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top