skip to Main Content

I have a custom WooCommerce product type.
If you switched between the standard WooCommerce type the input are cleared but this does not work for custom types.

So I created a jQuery for this

$('#product-type').on('change', function() {
  //do something
});

But this is not the solution because the functions in this code are also running if the page is refreshed.

What can I do to let this work only if the product type is changing

2

Answers


  1. Put your code inside the ready event. The code inside this function fires only when the DOM is completely loaded.

    $(document).ready(function(){
    
        $('#product-type').on('change', function() {
            //do something
        });
    
    });
    
    Login or Signup to reply.
  2. WooCommerce set the product type with JavaScript and is not set wit PHP.
    They use the change function if the page is loading and set the right product type on screen.

    If you need a function on a specific product type you can do something like this

    $('#product-type').on('change', function() {
        if($('#product-type').val() == '//your type'){
            //do something
        }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search