Jump to content

[SOLVED] getElementById() Not working in IE Why?? and NaN error


Recommended Posts

Hello,

 

I have the code below that calculates the totals of the selected / quantity of items in the cart. The code works fine on all browsers but for IE 6, 7 and partially works in 8. I get an Unknown runtime error, line 123, char 4, code 0. I do not understand why its is happening? I have checked and confirmed that variables that I pass to the function is correct! From the debugging that I did it seems that the problem is with outputResult?

 

The error I am getting in IE 8 is a Nan error Total function? Any ideas?

 

function itemTotal(phQuantity,saleOption,eKind,outputResult){
theSubTotal = phQuantity.value * saleOption;
theSubTotal = theSubTotal.toFixed(2); // round the subtotal 2 decimal points
// check if item has a checkbox
if(eKind == "checkbox"){
	if(phQuantity.checked){
		document.getElementById(outputResult).innerHTML = "$"+theSubTotal;
		document.getElementById(outputResult+'-hidden').value = theSubTotal;
		Total(document.frmOrder,"subItem");
	}else{
		document.getElementById(outputResult).innerHTML = " ";
		document.getElementById(outputResult+'-hidden').value = null;
		Total(document.frmOrder,"subItem");
	}
}else{
	if(phQuantity.value >= 1){
		document.getElementById(outputResult).innerHTML = "$"+theSubTotal; ERROR HERE ON THIS LINE
		document.getElementById(outputResult+'-hidden').value = theSubTotal;
		Total(document.frmOrder,"subItem");

	}else{
		document. getElementById(outputResult).innerHTML = " ";
		document.getElementById(outputResult+'-hidden').value = null;
		Total(document.frmOrder,"subItem");
	}
}
}


var TotalValue;
function Total(field,fieldClass){
TotalValue = 0;
var thisField = field.elements;
for (var i = 0; i < thisField.length; i++){
	if (thisField[i].className == fieldClass){	
		if (thisField[i].value.length > 0){
			TotalValue += parseInt(thisField[i].value);
		}
	}
}
document.getElementById('frmTotal').innerHTML = "$"+TotalValue.toFixed(2);
document.getElementById('frmGst').innerHTML = "$"+(TotalValue*12.5/100).toFixed(2);
document.getElementById('frmTotalIncl').innerHTML = "$"+(TotalValue*12.5/100+TotalValue).toFixed(2);

}

I am sorry itemResult() where is that in my function?

 

Sorry, typo. Where and how is itemTotal() called?

 

Hi that function is called on another php page. There is a lot of code to show if you wanna see the whole thing. That said I think this is what you are looking for

 

<input type="text" name="<? echo $phID ?>qty-<? echo $options['optionID']?>opt" id="<? echo $phID ?>qty" size="2" onkeyup ="itemTotal(this,'<? echo $options['optionPrice']?>','text','<? echo $phID ?>qty-<? echo $options[optionID]?>opt')" />

 

Here's your problem. You need to change this:

 

<input type="text" name="<? echo $phID ?>qty-<? echo $options['optionID']?>opt" id="<? echo $phID ?>qty" size="2" onkeyup ="itemTotal(this,'<? echo $options['optionPrice']?>','text','<? echo $phID ?>qty-<? echo $options[optionID]?>opt')" />

 

to this:

 

<input type="text" name="<? echo $phID ?>qty-<? echo $options['optionID']?>opt" id="<? echo $phID ?>qty" size="2" onkeyup ="itemTotal("id="<?php echo $phID ?>qty",'<? echo $options['optionPrice']?>','text','<? echo $phID ?>qty-<? echo $options[optionID]?>opt')" />

 

Note that I used <?php and not <?. You shouldn't use short tags, so you should actually make all your short tags into full php tags.

Ahh sorry, I was going off memory, and it turns out my memory was wrong. I thought that 'this' was supposed to be the ID that you were using for getElementById() in your javascript function, which will fail. But now that I look back at your original function, that wasn't the reference to the ID.

 

Can you show the HTML output of that code, rather than the PHP? Since it is javascript, the PHP is irrelevant.

Sure man this is what the php will output.

<input type="text" name="95qty-7opt" id="95qty" size="2" onkeyup ="itemTotal(this,'105.00','text','95qty-7opt')" />

 

Here is a sample page with the code running

http://www.smart-photography.co.nz/clientSideSelectOptions2.php

 

Thank you very much really but I got it working finally. The solution was

 

1 declare the theSubTotal variable

 

var theSubTotal;

 

2 do a parseFloat()

 

theSubTotal = parseFloat(phQuantity.value) * parseFloat(saleOption);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.