Change quantity via jQuery and disabile quantity field

I have products where I need to disable the quantity field on the product detail pages and dynamically change the quantity based on another value.

I change the value using this code:

if (jQuery('body').hasClass('is-adapt-quantity-product')) {
            jQuery('.wc-pao-addon-custom-textarea').bind('keyup', _.debounce(function(){
                jQuery(.qty).val(parseInt(jQuery(this).val().length));
            }, 500));
    }

This works fine. The quantity changes and if I click on the add to cart button, I have the quantity in my cart.

When I add this to the js code:

jQuery(.qty).attr(disabled, disabled);

The quantity on the product detail page changes. However when I add the product to the cart, it adds always just the quantity of 1.

Does anybody know why this happens and how I can prevent this from happening? I want to disable the quantity field, because the field should be changes dynamically...

Topic jquery Wordpress

Category Web


If the input is disabled, it is ignored during form submission. You can try to use readonly attribute.

As it says in the docs: The difference between disabled and readonly is that read-only controls can still function and are still focusable, whereas disabled controls can not receive focus and are not submitted with the form and generally do not function as controls until they are enabled.

Reference: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/readonly

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.