I have built an order inquiry form that has a few Javascript-driven features. I've got this form working on every browser I can test, which is Win 10 and a few Mac operating systems. My client is still using Windows XP, and he says these two features are not working:
increase price of an item with a radio button option
calculate subtotal at the bottom of the page after collecting all of the prices and quantities from the various items.
Before I tell him that I can't support XP, I'm wondering if there's something in my script that is a known to fail in Firefox on Windows XP (and hopefully has a workaround). I've checked the method documentation online, and I'm not seeing it. Here are the various scripts that I'm using. These are pure Javascript, just to see if I can do it.
The subtotal recalculation is triggered every time an item quantity changes.
/************************************************
* Wide Option
************************************************/
function subtractWideOption() { // below traverse DOM from radio button to price
var priceSpan = event.target.parentNode.parentNode.nextElementSibling.firstElementChild;
if (priceSpan.className.indexOf("wider") !== -1) { // test if option is already set
var currentPrice = parseInt(priceSpan.innerHTML.slice(1), 10); // remove $ sign and parse
var newPrice = currentPrice - 25; // remove cost for option
priceSpan.innerHTML = "$" + newPrice; // add $ sign
priceSpan.className = priceSpan.className.replace(" wider",""); // remove option class
priceSpan.className += " std"; // add standard class
} else {
return;
}
}
function addWideOption() {
var priceSpan = event.target.parentNode.parentNode.nextElementSibling.firstElementChild;
if (priceSpan.className.indexOf("std") !== -1) {
var currentPrice = parseInt(priceSpan.innerHTML.slice(1), 10);
var newPrice = currentPrice + 25;
priceSpan.innerHTML = "$" + newPrice;
priceSpan.className = priceSpan.className.replace(" std","");
priceSpan.className += " wider";
} else {
return;
}
}
/************************************************
* Order Subtotal
************************************************/
var qtyBtns = document.getElementsByClassName("qty"); // collect all qty inputs
var subTotal = document.getElementById("oi_subt");
function recalcSubtotal() {
var subT = 0;
for (var i = 0; i < qtyBtns.length; i++) { // below traverse DOM from input to price
var priceHTML = qtyBtns[i].parentNode.previousElementSibling.firstElementChild.innerHTML;
if (priceHTML == "TBD") { // one custom item with TBD price is ignored
price = "0"
} else {
priceHTML = priceHTML.replace(",",""); // remove comma from price
var price = parseInt(priceHTML.slice(1), 10); // remove $ sign and parse
};
var qty = parseInt(qtyBtns[i].value, 10);
subT += (price * qty); // add up all items ordered
subTotal.innerHTML = "$" + subT;
}
}
You are binding your event handlers like this:
var stepupBtns = document.getElementsByClassName("steppingUp");
for (var i = 0; i < stepupBtns.length; i++) {
stepupBtns[i].onclick = function(){
var valueOf = this.previousElementSibling.value;
valueOf = ++valueOf;
this.previousElementSibling.value = valueOf;
event.preventDefault();
recalcSubtotal();
}
}
In these functions, note that the event
variable is not defined anywhere. The event variable should be passed in your function definition:
stepupBtns[i].onclick = function(event){
You should do this on all of your handlers.
More information can be found here: https://stackoverflow.com/a/20522980