any javascript experts here? i need help!
I'm trying to integrate form checkbox functionality into this script that was built for drop down menus. I passing an onClick event with the price as a value.
Can someone help? Thanks.
PHP Code:
onClick="showPrice($100);"
PHP Code:
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 = "Subtotal Price with Options $" + myTotalPrice;
}
}
I'm assuming since you're using checkboxes, that there will be more than one selectable item on the screen (more than one checkbox) and that the displayed price would need to be updated when a user checks or unchecks the checkbox, yes?
There are several ways to do this, but if there aren't going to be hundreds of checkboxes on the screen, probably the easiest way to do this is to have the checkbox code look like this:
Then change the script to this (assumes that there is only one form on the page):
The problem with the original script is that it's trying to do a bit more than this. For instance, it utilizes a FormatNumber() function you don't list here.
Hope this helps!
There are several ways to do this, but if there aren't going to be hundreds of checkboxes on the screen, probably the easiest way to do this is to have the checkbox code look like this:
PHP Code:
<input type="checkbox" name="fieldName" value="100" onClick="showPrice()">"
PHP Code:
function showPrice()
{
var totalPrice = 0;
var form = document.forms[0];
for(i=0;i<form.elements.length;i++)
{
if(form.elements[i].type == checkbox && form.elements[i].checked == true)
{
totalPrice += parseFloat(form.elements[i].value);
}
}
document.getElementById("productNEWprice").innerHTML = "Subtotal Price with Options $" + totalPrice;
}
Hope this helps!
Thanks AgS2K... Basically this is part of a complex BTO Computer Configurator script. I'm trying to use it to automatically update the display price as the user selects different options.
My main problem is getting this to work with the drop down menus as well as the checkboxes.
The FormatNumber() function is this:
Thanks for your help, I'll see if I can integrate it together.
My main problem is getting this to work with the drop down menus as well as the checkboxes.
The FormatNumber() function is this:
PHP Code:
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);
}
Then I would suggest having the "totalPrice" variable as a global variable (initially set outside of any function). If you can't integrate the two showPrice() scripts, have two of them, named differently, of course, one for the drop downs, one for the checkboxes, that each update the "totalPrice" variable and write the value onto the screen.
But there are at least half-a-dozen other ways to do this...
But there are at least half-a-dozen other ways to do this...
Thread
Thread Starter
Forum
Replies
Last Post



