0

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:

  1. increase price of an item with a radio button option

  2. 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;
  }
}
chillywilly
  • 405
  • 3
  • 11
  • What exactly fails in the script there? What is the error? – CertainPerformance Nov 28 '19 at 00:37
  • Unfortunately, I have limited access to my client's view. I don't want to bug him too much. He just says they aren't working. I assume he's not getting an error message. Here's a link to my form in progress in case you have XP and Firefox... https://www.greatgraphicdesign.com/work/bsp/order-inquiry.html – chillywilly Nov 28 '19 at 00:41
  • Have you checked your methods on caniuse? like: https://caniuse.com/#search=slice – Chase Choi Nov 28 '19 at 00:44
  • Many distinct versions of Firefox ran on Windows XP (an operating system that has been without security or any other kind of update for over five years). Finding out exactly which version of Firefox is involved would be a really good idea. – Pointy Nov 28 '19 at 00:44

1 Answers1

1

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

  • If you've identified the error, and it's been answered before, best to flag to close as a duplicate of the linked question instead – CertainPerformance Nov 28 '19 at 01:03
  • Thanks! That's helpful feedback. In this particular case, the increment/decrement scripts were working in XP so they aren't giving me the headache. I have removed them from my original question so we can eliminate some clutter. What's left are the two scripts that are failing (only in old Firefox). – chillywilly Nov 28 '19 at 01:04
  • 1
    @chillywilly I booted up Firefox on XP using Browserstack, and can confirm this is your issue. I may have linked the wrong function. `event` is not defined anywhere and FF is throwing an error. Find all of your `onclick` handlers and ensure that they are passing the argument `event` in the function definition. You can view the lined post for more information. – Josh Stabback Nov 28 '19 at 01:12
  • Just read your updated response. Thank you so much! I'm hopeful. So should I add event like this? function addWideOption(event) { --- or should I place event in the HTML like this? onclick="subtractWideOption(event)" -- or both? – chillywilly Nov 28 '19 at 01:17
  • 1
    The former, I have an example in my answer. The website Browserstack is a great site to help test cross platform, it is what I used. – Josh Stabback Nov 28 '19 at 01:24
  • Adding event to the function call in the script ("the former" in my comment above) resulted in a console error, "Uncaught TypeError: Cannot read property 'target' of undefined." Adding it to both sides works, at least in my browsers. I'll have to check with my client re: Firefox. I'm not understanding this well enough, and I am most grateful for your assistance! – chillywilly Nov 28 '19 at 01:51
  • My client says that everything is working now! The solution was, as Josh pointed out, passing the event variable to the function. I had to place the word "event" in both the HTML and in the .js file since I have an inline onclick event calling the function. Thank you! – chillywilly Nov 28 '19 at 04:46
  • Happy to help. Feel free to upvote the answer and mark as accepted! – Josh Stabback Nov 28 '19 at 14:03