0

am on Drupal 8. Actually I use this code in order have plus and minus buttons (for quantity in add to cart)

    var spinner = $(this),
    input = spinner.find('input[type="number"]'),
    btnUp = spinner.find('.quantity-up'),
    btnDown = spinner.find('.quantity-down'),
    min = input.attr('min'),
    max = input.attr('max');
    var step = input.attr('step');
    step = $.isNumeric(step) ? parseFloat(step) : 1;
    var scale = step.toString().split('.');
    scale = scale[1] ? scale.pop().length : 0;

  btnUp.click(function() {
    var oldValue = parseFloat(input.val());
    if (oldValue >= max) {
      var newVal = oldValue;
    } else {
      var newVal = oldValue + step;
    }
    spinner.find("input").val(parseFloat(newVal)).toFixed(scale);
    spinner.find("input").trigger("change");
  });

  btnDown.click(function() {
    var oldValue = parseFloat(input.val());
    if (oldValue <= min) {
      var newVal = oldValue;
    } else {
      var newVal = oldValue - step;
    }
    spinner.find("input").val(parseFloat(newVal)).toFixed(scale);
    spinner.find("input").trigger("change");
  });

The code work as expected, but Firefox and Chrome says:

TypeError: t.find(...).val(...).toFixed is not a function

Any ideas to solve this? Ty for answer!

AmeDSL
  • 11
  • 1

1 Answers1

0

Your parenthesis is closed too early in two locations in your code:

spinner.find("input").val(parseFloat(newVal)).toFixed(scale);

It should be closed at the end like so:

spinner.find("input").val(parseFloat(newVal).toFixed(scale));
Vindur
  • 364
  • 3
  • 12
  • Ty for quick answer. With your code the error gone, but the numbers counter got crazy (actually increment by 10 digit...like 1-11-21) actually am using INTEGER values, with decimal one it's ok, but I need a number without zeros (like 1 not 1.0 ) – AmeDSL Jun 30 '20 at 14:13