Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Need help changing jquery to javascript for an on change function

I am trying to change some jQuery code to plain JS for an on call function. I keep getting the following error – Failed to execute ‘addEventListener’ on ‘EventTarget’: parameter 2 is not of type ‘Object’.

Old

$(document).on('change', '.js-variant-radio', onVariantRadioChange);

New

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

document.querySelector('.js-variant-radio').addEventListener("change", onVariantRadioChange);

Complete code block here

(function(){
  //START QUANTITY INPUT VALIDATION FUNCTION
  let
    onVariantRadioChange = function(event) {
      let
        $radio = document.querySelector(this),
        $form = $radio.closest('form'),
        max = $radio.attr('data-inventory-quantity'),
        $quantity = $form.querySelector('.js-quantity-field'),
        $addToCartButton = $form.querySelector('.add-to-cart-button');

      if($addToCartButton.prop('disabled') === true) {
        $addToCartButton.prop('disabled', false);
      }

      $quantity.attr('max', max);

      if (parseInt($quantity.value) > max) {
        $quantity.val(max).change();
      }
    };

    //END QUANTITY INPUT VALIDATION FUNCTION
})();

>Solution :

You’re still adding the event listener with jQuery. Try changing it to:

const jsVariantRadios = document.getElementsByClassName('.js-variant-radio');

jsVariantRadios.addEventListener('change', onVariantRadioChange);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading