Forum Moderators: open
Would someone please advise why the following piece of simple code displays the line 'Total Price with Options $999.99' under Internet Explorer, but it will not display in Netscape until i have selected one of the the drop downs?
Many thanks
<head>
<title> Small JavaScript Example </title>
<script language="javascript">
<!--
function FormatNumber(num)
{
if(isNaN(num)) { num = "0"; }
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10) { cents = "0" + cents; }
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
{
num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
}
return (((sign)?'':'-') + num + '.' + cents);
}
function showPrice(form)
{
var myTotalPrice = 0;
var showUP = 0;
var myMathProblem = "";
myItemPrice = parseFloat(form.nuPrice.value);
for (var i = 0; i < form.elements.length; i++)
{
var e = form.elements[i];
if ( e.type == 'select-one' )
{
showUP = 1;
Item = e.selectedIndex;
myPrice = e.options[Item].text;
myDollarSign = myPrice.indexOf("$",0)
if ( myDollarSign!= "-1" )
{
myParSign = myPrice.indexOf(")", myDollarSign);
myAttributeString = myPrice.substring(myDollarSign+1, myParSign);
myAttributeString = myAttributeString.replace(/,/,"");
myAttributePrice = parseFloat(myAttributeString);
myMathProblem = myPrice.charAt(myDollarSign - 1);
} else { myAttributePrice = 0; }
if (myMathProblem == "+")
{
myTotalPrice = myTotalPrice + myAttributePrice;
} else {
myTotalPrice = myTotalPrice - myAttributePrice;
}
}
}
if ( showUP )
{
myTotalPrice = FormatNumber(myTotalPrice + myItemPrice);
document.getElementById("productNEWprice").innerHTML = "Total Price with Options $" + myTotalPrice;
}
}
//-->
</script>
</head>
<html>
<body onload='showPrice(cart_quantity)'>
<form name="cart_quantity">
<input type="hidden" name="nuPrice" value="999.99">
<select name="brian[]" onChange="showPrice(this.form);">
<option> Select One </option>
<option value="1">10 (+$1315.00)</option>
<option value="2"> 2 (+$20.00) </option>
</select>
<select name="brian[]" onChange="showPrice(this.form);">
<option> Select One </option>
<option value="1"> 1 (+$15.00)</option>
<option value="2"> 2 (+$10.00) </option>
<option value="3"> 3 (-$21.00) </option>
</select>
<br><br><div id="productNEWprice"></div>
</form>
</body>
</html>
and a warm welcome to these forums ;)
Try this modified version...
<html>
<head>
<title> Small JavaScript Example </title>
<script type="text/javascript">
<!--
function FormatNumber(num)
{
if(isNaN(num)) { num = "0"; }
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10) { cents = "0" + cents; }
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
{
num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
}
return (((sign)?'':'-') + num + '.' + cents);
}
function showPrice()
{
var myTotalPrice = 0;
var showUP = 0;
var myMathProblem = "";
myItemPrice = parseFloat(document.forms[0].nuPrice.value);
for (var i = 0; i < document.forms[0].elements.length; i++)
{
var e = document.forms[0].elements[i];
if ( e.type == 'select-one' )
{
showUP = 1;
Item = e.selectedIndex;
myPrice = e.options[Item].text;
myDollarSign = myPrice.indexOf("$",0)
if ( myDollarSign!= "-1" )
{
myParSign = myPrice.indexOf(")", myDollarSign);
myAttributeString = myPrice.substring(myDollarSign+1, myParSign);
myAttributeString = myAttributeString.replace(/,/,"");
myAttributePrice = parseFloat(myAttributeString);
myMathProblem = myPrice.charAt(myDollarSign - 1);
} else { myAttributePrice = 0; }
if (myMathProblem == "+")
{
myTotalPrice = myTotalPrice + myAttributePrice;
} else {
myTotalPrice = myTotalPrice - myAttributePrice;
}
}
}
if ( showUP )
{
myTotalPrice = FormatNumber(myTotalPrice + myItemPrice);
document.getElementById("productNEWprice").innerHTML = "Total Price with Options $" + myTotalPrice;
}
}
//-->
</script>
</head>
<body onload='showPrice()'>
<form action="">
<input type="hidden" name="nuPrice" value="999.99">
<select name="brian[]" onChange="showPrice();">
<option> Select One </option>
<option value="1">10 (+$1315.00)</option>
<option value="2"> 2 (+$20.00) </option>
</select>
<select name="brian[]" onChange="showPrice();">
<option> Select One </option>
<option value="1"> 1 (+$15.00)</option>
<option value="2"> 2 (+$10.00) </option>
<option value="3"> 3 (-$21.00) </option>
</select>
<br><br><div id="productNEWprice"></div>
</form>
</body>
</html> birdbrain
I am afraid that my skills are minor,
and with regards to PHP non-existent ;)
It may be prudent to take this problem
to the 'PHP Server Side Scripting' forum
and hope for some assistance there.
birdbrain