Forum Moderators: open
the following piece of code displays 2 field choosers, which contain pricing both in $ and £ - at the moment the price only changes when the price with the £ is selected. I would like both currencies supported so the price changes when either is selected. At the moment it is the first currency of this line: myDollarSign = myPrice.indexOf("£") ¦¦ myPrice.indexOf("$")
so that being the £
Code as below:
Many thanks.
<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 = "";
if (document.main!= null)
{
myItemPrice = parseFloat(document.main.nuPrice.value);
for (var i = 0; i < document.main.elements.length; i++)
{
var e = document.main.elements[i];
if ( e.type == 'select-one' )
{
Item = e.selectedIndex;
myPrice = e.options[Item].text;
myDollarSign = myPrice.indexOf("£") ¦¦ myPrice.indexOf("$")
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;
}
}
}
}
else
{
myTotalPrice = 0;
myItemPrice = 0;
}
myTotalPrice = FormatNumber(myTotalPrice + myItemPrice);
document.getElementById("productNEWprice").innerHTML = "Total Price with Options $" + myTotalPrice;
document.write(myDollarSign)
}
//-->
</script>
</head>
<body onload='showPrice()'>
<form id="main" name="main" 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>
myDollarSign = myPrice.indexOf("£") ¦¦ myPrice.indexOf("$")
...later...
if ( myDollarSign!= "-1" )
This is doubly wrong (if that's possible). The term below will produce an integer (-1, if £ isn't present)
myPrice.indexOf("£") "-1" is a string. So
myDollarSign!= "-1" will always be true. This assignment statement..
myDollarSign = myPrice.indexOf("£") ¦¦ myPrice.indexOf("$") ..will produce a boolean (true or false). But, at the moment, the values either side of the ¦¦ are the integers produced by those terms. All values, except for zero, are considered to be true. The entire RHS will only give false if both values are zero, which means that both £ and $ are the first character. This is impossible. We need the terms on either side of the ¦¦ to give correct boolean values. Use the code I gave before.
In fact, we might as well check, not just for the presence of the character, but make sure that it's at the first position. So the relevant bits are:
myDollarSign = myPrice.indexOf("£")==0 ¦¦ myPrice.indexOf("$")==0
...later...
if ( myDollarSign )
NB: Don't forget to change the ¦¦ to unbroken pipes.
Good luck.
I changed the pipes and added your code, no errors but when i select the drop down boxes, no price is displayed. My Previous syntax of:
myDollarSign = myPrice.indexOf("£") ¦¦ myPrice.indexOf("$")
Displayed the price change when the following drop down was selected:
<option value="2"> 2 (+£20.00) </option>
Therefore i figured it was ignoring everything to the RHS of the ¦¦ as when i selected anything price option with $ there was no change in price.
Just to re-cap , i need to have the price calculate regardless of currency options selected.
i now have the following code with your line change in it:
<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 = "";
if (document.main!= null)
{
myItemPrice = parseFloat(document.main.nuPrice.value);
for (var i = 0; i < document.main.elements.length; i++)
{
var e = document.main.elements[i];
if ( e.type == 'select-one' )
{
Item = e.selectedIndex;
myPrice = e.options[Item].text;
myDollarSign = myPrice.indexOf("£")==0 ¦¦ 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;
}
}
}
}
else
{
myTotalPrice = 0;
myItemPrice = 0;
}
myTotalPrice = FormatNumber(myTotalPrice + myItemPrice);
document.getElementById("productNEWprice").innerHTML = "Total Price with Options $" + myTotalPrice;
}
//-->
</script>
</head>
<body onload='showPrice()'>
<form id="main" name="main" 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>
Thanks once again for your help, almost there i think.
cheers
Marcus