function totalPrice()
{
var totalItems = document.getElementsByName("price").length;
var total = 0;
var rgx = /[^0-9]\x2e$/g;
var price;
var string_in = document.getElementsByName("price");
for (var x=0; x<totalItems-1; x++){
price = new Number(rgx.exec(string_in[x].text));
parseFloat(total) = parseFloat(total) + parseFloat(price); //This is where the error occurs
}
document.getElementById("total").innerHTML = ' $' + total;
}
This is my function that the script executes, There are x number of items in my ‘cart’. we must write this function in Javascript only. No php. The Named elements contain a price in the form of $123.45 (or more digits)
Erm… just remove the
parseFloat()from around the left-hand side.Side-note: Always work in integers, especially when it comes to money. In this case, have your prices in cents on the code side. Only divide by 100 (or even just insert a decimal point via string functions) at the very end to display the result in dollars. This will avoid floating-point inaccuracies.